C#操作SQL Server中的Image类型数据

该例子是一个对SQL Server数据类型的一个操作例子,具有写入、读取功能。

1:准备数据库

1)创建数据库 Test

2)创建表 Table_1 (分别有2个字段:id(Int)photo(Image)

如图:




2:用C#进行读写操作,完整代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace imageTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_brewse_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Title = "浏览图像文件";
            op.Filter = "图像文件(*.jpg)|*.jpg";
            op.ShowDialog();
            txt_ImageAddress.Text = op.FileName;
        }

        private void btn_Insert_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(txt_ImageAddress.Text,FileMode.Open,FileAccess.Read);
            byte[] byteArray = new byte[fs.Length];
            fs.Read(byteArray,0,Convert.ToInt32(fs.Length));
            fs.Close();

            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
            SqlConnection conn = new SqlConnection(connectionStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Table_1(photo) VALUES(@photo)",conn);
            SqlParameter parmeter = new SqlParameter("@photo", SqlDbType.Image);
            parmeter.Value = byteArray;
            cmd.Parameters.Add(parmeter);
            int result = cmd.ExecuteNonQuery();
            if (result > 0)
                MessageBox.Show("插入成功");
            conn.Close();
        }

        private void btn_ReadImage_Click(object sender, EventArgs e)
        {
            string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
            SqlConnection conn = new SqlConnection(connectionStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM Table_1",conn);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            byte[] image = (byte[])dr["photo"];
            conn.Close();
            if (image.Length == 0)
                return;
            string photoUrl = Environment.CurrentDirectory + "\\1.jpg";
            FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.BaseStream.Write(image, 0, image.Length);
            bw.Flush();
            bw.Close();

            picbox_image.ImageLocation = photoUrl;
        }

    }
}


效果如图:


完整项目(包含数据库文件)下载地址:http://download.csdn.net/source/3576866

也可以留下Email,我会第一时间发给你。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andrew_wx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值