ADO.Net写入&读取图片文件

 

使用带参数的sql语句实现添加功能

        public static void InsertPic(string filename)
        {
            string sql = "insert into ImgTest(Name,Sculpture) values(@Name,@Sculpture)";
            byte[] ib = new Byte[10486760];
            FileStream fs = new FileStream(@filename, FileMode.Open, FileAccess.Read);
            fs.Read(ib, 0, 10486760);
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("Name", SqlDbType.VarChar, 50),
                new SqlParameter("@Sculpture", SqlDbType.Image, (int)fs.Length)
            };
            param[0].Value = "AA";
            param[1].Value = ib;

            int result = SQLHelper.Update(sql, param);
        }

根据名称返回单一图片

        public static Image GetPic(string Id)
        {
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string sql = "select Sculpture from ImgTest where Name = '{0}'";
            sql = string.Format(sql, Id);
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            MemoryStream buf = new MemoryStream((byte[])reader[0]);
            Image image = Image.FromStream(buf, true);
            return image;
        }

修改特定对象的图片内容

        public static void Change(string filename,string Name)
        {
            string sql = "update ImgTest set Sculpture = @Sculpture where Name = @Name";
            byte[] ib = new Byte[10486760];
            FileStream fs = new FileStream(@filename, FileMode.Open, FileAccess.Read);
            fs.Read(ib, 0, 10486760);
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("Name", SqlDbType.VarChar, 50),
                new SqlParameter("@Sculpture", SqlDbType.Image, (int)fs.Length)
            };
            param[0].Value = Name;
            param[1].Value = ib;

            int result = SQLHelper.Update(sql, param);
        }

从数据库中获取某图片

        public static Image GetPic(string Id)
        {
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string sql = "select Sculpture from ImgTest where Name = '{0}'";
            sql = string.Format(sql, Id);
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            MemoryStream buf = new MemoryStream((byte[])reader[0]);
            Image image = Image.FromStream(buf, true);
            return image;
        }

从数据库中获取图片组


            string sql1 = "select Sculpture from ImgTest";
            DataSet ds1 = SQLHelper.GetDataSet(sql1);
            foreach (DataRow item in ds1.Tables[0].Rows)
            {
                obj.Add(ImageConvert.ArryToPic((byte[])item.ItemArray[0]));
            }

将含图片的DateSet映射到控件

            string sql = "select *from ImgTest";
            DataSet ds = SQLHelper.GetDataSet(sql);
            DataShow.DataSource = ds.Tables[0];

效果

附测试工程文件:https://download.csdn.net/download/weixin_37878740/12293188

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值