sql server数据库 写入图片

来源:互联网 发布:作文软件 编辑:程序博客网 时间:2024/05/16 09:57

首先:数据库中保存图片的字段类型设置为Image.注:设成byte也可以,但是其存储的图片大小有限制

 

编写代码:

                private SqlConnection conn = new SqlConnection();

                conn.ConnectionString = "Data Source=findleaf;Database=match;UID=sa;PWD=sa;";        

                SqlCommand cmd = new SqlCommand(sql, conn);
                System.IO.StreamReader SR = new System.IO.StreamReader(pic_MemberPic.ImageLocation);
                System.IO.Stream strea = SR.BaseStream;
                byte[] imgdata = new byte[strea.Length];
                strea.Read(imgdata, 0, (int)strea.Length);
                cmd.Parameters.Add("@imgdata", System.Data.SqlDbType.Binary).Value = imgdata;
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("提示添加成功", "提示");