今天是:   | 登录中心 | | 网络导航
logo 来海南旅游咨询 海南旅游 预订海南酒店 自驾租车 海南高尔夫 问路订房就请拔打海南旅游百事通  全天24小时为您守候 设为首页
加入收藏
联系站长
导航页为首页
内容页为首页
当前位置:南国风 >> IT.信息技术 >> c#web开发 >> 浏览文章

c#将图像保存到SQL server2000的Image字段中

作者:佚名 日期:2012年02月01日 来源:本站原创 人气:

将图像保存到SQL server2000的Image字段中

private void button2_Click_1(object sender, System.EventArgs e)

{

string pathName;

if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)

{

pathName = this.openFileDialog1.FileName;

System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);

this.pictureBox1.Image = img;

//将图像读入到字节数组

System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

byte[] buffByte = new byte[fs.Length];

fs.Read(buffByte,0,(int)fs.Length);

fs.Close();

fs = null;

//建立Command命令

string comm = @"Insert into table1(img,name) values(@img,@name)";

this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();

this.sqlCommand1.CommandType = System.Data.CommandType.Text ;

this.sqlCommand1.CommandText = comm;

this.sqlCommand1.Connection = this.sqlConnection1 ;

//创建Parameter

this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);

this.sqlCommand1.Parameters[0].Value = buffByte;

this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

this.sqlCommand1.Parameters[1].Value =pathName.Substring(pathName.LastIndexOf("\\")+1);

try

{

this.sqlConnection1.Open();

this.sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

}

catch(System.Exception ee)

{

MessageBox.Show(ee.Message );

}

buffByte = null;

this.FillListBox();

}

读取:

数据库读图片到picturebox

SqlConnection conn=new SqlConnection(@"data source=chenyuming2004VSdotNET;uid=sa;pwd=cym;database=lhf");

conn.Open();

SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);

SqlDataReader reader=cmd.ExecuteReader();

reader.Read();

MemoryStream buf=new MemoryStream((byte[])reader[0]);

Image image=Image.FromStream(buf,true);

pictureBox1.Image=image;


email:lifat@yeah.net| QQ:_3000_99**61 T先生
  服务指南 | 著作权与商标声明 | 法律声明 | 服务条款 | 隐私声明 |


南国风网站的版权信息:保留所有权力

不良信息举报中心!
琼ICP备15001822号