将二进制文件写入sql数据库

该博客介绍如何使用C#将二进制文件(如图片)读取为字节数据,并通过SqlConnection和DataSet保存到SQL数据库的特定表中。通过FileStream打开文件,读取内容到byte数组,然后创建新的数据行并更新到数据库。
摘要由CSDN通过智能技术生成

 SqlConnection conn=null;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
                conn = new SqlConnection(connectionutil.getconnetionstr());
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter("select * from tb_image", conn);
                SqlCommandBuilder mycb = new SqlCommandBuilder(da);
                DataSet ds = new DataSet("MyImage");
                da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                FileStream fs = new FileStream("D://北京之旅1.jpg", FileMode.OpenOrCreate, FileAccess.Read);
                byte[] MyData = new byte[fs.Length];
                fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();
             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值