sqlserver数据库的image字段存入方法 ,读取

本文介绍了如何在SQLServer数据库中存入和读取image类型字段,提供了详细的存取步骤,帮助开发者掌握该类型数据的操作技巧。
摘要由CSDN通过智能技术生成


存入方法

private void button1_Click(object sender, EventArgs e)
        {
            openFileImage.Filter = "*.jpg|*.JPG|*.gif|*.GIF|*.bmp|*.BMP";

            if (openFileImage.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.pictureBox1.Image = Image.FromStream(this.openFileImage.OpenFile());
                    string strimg = openFileImage.FileName.ToString();
                    //获取文件对话框中选定的文件名的字符串,包括文件路径 

                    FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    imgBytesIn = br.ReadBytes((int)fs.Length);
                    //fs.Length文件流的长度,用字节表示 
                    //从当前流中将count个字节读入字节数组中
                    //打开数据库
                    SqlConnection con = new SqlConnection("server=192.168.1.164,1433;uid=sa;pwd=mas_lxw;database=Relocation_DT");
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update gytd set xytp=@Image", con);
                    cmd.Parameters.Add("@Image", SqlDbType.Image);
                    cmd.Parameters["@Image"].Value = imgBytesIn;
                    cmd.ExecuteNonQuery();

                    con.Close();
                    MessageBox.Show("图片上传成功"); 

                }
                catch
                {
                    MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.pictureBox1.Image = null;
                }
               
            }
        }      

<
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值