日期:2014-05-16  浏览次数:20933 次

请教一下如何在mssql中二进制储存图片
本帖最后由 oLaoJiu 于 2014-03-30 20:57:18 编辑
从网上找了一段代码 每次储存一张图片的时候是正确的
 FileStream fs = new FileStream(txt_图片路径.Text, FileMode.Open);
                byte[] imagebytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                fs.Close();
                //打开数据库
                SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=xxxxx;database=减速机内容管理系统");
                con.Open();
                SqlCommand com = new SqlCommand("insert into 图片列表 values(@ImageList,'','')Select @@identity", con);
                com.Parameters.Add("ImageList", SqlDbType.Image);
                com.Parameters["ImageList"].Value = imagebytes;
                int Gid = Convert.ToInt32(com.ExecuteScalar());
                con.Close();
                lab_图片存入提示.Text = "存入提示:储存成功,ID:" + Gid;


后来我改了一下 需要一次储存7张图片,但是储存到数据库中的图片长度是错误的,麻烦大侠帮忙看下是哪里的问题,万分感谢,找了一天也没有找到问题

下面是一次储存多张的代码(中间删除了重复的代码)

 //装配型式
                FileStream fs装配型式 = new FileStream(txt_装配型式图片路径.Text, FileMode.Open);
                byte[] imagebytes装配型式 = new byte[fs装配型式.Length];
                BinaryReader br装配型式 = new BinaryReader(fs装配型式);
                imagebytes装配型式 = br装配型式.ReadBytes(Convert.ToInt32(fs装配型式.Length));
                fs装配型式.Close();

                //选型标记
                FileStream fs选型标记 = new FileStream(txt_选型标记图片路径.Text, FileMode.Open);
                byte[] imagebytes选型标记 = new byte[fs选型标记.Length];
                BinaryReader br选型标记 = new BinaryRead