| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1332 人关注过本帖
标题:WINFORM在form框架中如何给SQL Server上传图片
只看楼主 加入收藏
aldfyl
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-1
收藏
 问题点数:0 回复次数:2 
WINFORM在form框架中如何给SQL Server上传图片
请问大家,我初学C#,现在想把照片上传到数据库,具体步骤是怎样呢,我该怎么做????????
需要用到什么知识点????
搜索更多相关主题的帖子: SQL form WINFORM Server 数据库 
2007-05-04 08:51
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
收藏
得分:0 

// 下面是从数据库取出图片
try
{
string getPhoto = @"select photo from gch_Customer_Photo where customerID=1234";

DataSet ds = new DataSet();
sqlDataAdapter1 = new SqlDataAdapter(getPhoto, sqlConnection1);
sqlDataAdapter1.Fill(ds);

DataRow row = ds.Tables[0].Rows[0];
byte[] bPhoto = new byte[0];
bPhoto = (byte[])row["photo"];
//int arraySize = bPhoto.GetUpperBound(0);

MemoryStream memstr = new MemoryStream(bPhoto);
pictureBox1.Image = Image.FromStream(memstr, true);

existPhoto==true
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

// 下面是将图片存入数据库

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.bmp;*.jpg;*.gif|*.bmp;*.jpg;*.gif;*.jpeg";
if(ofd.ShowDialog()==DialogResult.OK)
{
string filePath = ofd.FileName;

FileInfo imageFile = new FileInfo(filePath);
if(imageFile.Length > 204800)
{
Console.WriteLine("图片大小最好不要过大!");
}

pictureBox1.Image = Image.FromFile(filePath);

if(existPhoto==true)
{ //如果之前已有图片,可以考虑删除了旧的先
string deleteOld = @"delete from gch_Customer_Photo where customerID=1234";
SqlCommand sqlcmmd = new SqlCommand(deleteOld,sqlConnection1);
sqlcmmd.ExecuteNonQuery();
}

string getAllPhotos = @"select customerID, photo from gch_Customer_Photo";

DataSet ds = new DataSet();
sqlDataAdapter1 = new SqlDataAdapter(getAllPhotos,sqlConnection1);
sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;

FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);

byte[] bPhoto= new byte[fs.Length];
fs.Read(bPhoto, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

sqlDataAdapter1.Fill(ds);

DataRow oneRow = ds.Tables[0].NewRow();
oneRow["customerID"] = 1234;
oneRow["photo"] = bPhoto;
ds.Tables[0].Rows.Add(oneRow);

sqlDataAdapter1.Update(ds);

Console.WriteLine("图片入库成功!");
}


不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2007-05-04 10:20
wcp126
Rank: 1
等 级:等待验证会员
帖 子:95
专家分:7
注 册:2010-3-30
收藏
得分:0 
给个实例研究一下
914216007@
2010-10-04 15:21
快速回复:WINFORM在form框架中如何给SQL Server上传图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017303 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved