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

1:准备数据库

1)创建数据库 Test

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

如图:

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

[csharp] view
plain
 copy

print?

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. namespace imageTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void btn_brewse_Click(object sender, EventArgs e)
  20. {
  21. OpenFileDialog op = new OpenFileDialog();
  22. op.Title = "浏览图像文件";
  23. op.Filter = "图像文件(*.jpg)|*.jpg";
  24. op.ShowDialog();
  25. txt_ImageAddress.Text = op.FileName;
  26. }
  27. private void btn_Insert_Click(object sender, EventArgs e)
  28. {
  29. FileStream fs = new FileStream(txt_ImageAddress.Text,FileMode.Open,FileAccess.Read);
  30. byte[] byteArray = new byte[fs.Length];
  31. fs.Read(byteArray,0,Convert.ToInt32(fs.Length));
  32. fs.Close();
  33. string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
  34. SqlConnection conn = new SqlConnection(connectionStr);
  35. conn.Open();
  36. SqlCommand cmd = new SqlCommand("INSERT INTO Table_1(photo) VALUES(@photo)",conn);
  37. SqlParameter parmeter = new SqlParameter("@photo", SqlDbType.Image);
  38. parmeter.Value = byteArray;
  39. cmd.Parameters.Add(parmeter);
  40. int result = cmd.ExecuteNonQuery();
  41. if (result > 0)
  42. MessageBox.Show("插入成功");
  43. conn.Close();
  44. }
  45. private void btn_ReadImage_Click(object sender, EventArgs e)
  46. {
  47. string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";
  48. SqlConnection conn = new SqlConnection(connectionStr);
  49. conn.Open();
  50. SqlCommand cmd = new SqlCommand("SELECT * FROM Table_1",conn);
  51. SqlDataReader dr = cmd.ExecuteReader();
  52. dr.Read();
  53. byte[] image = (byte[])dr["photo"];
  54. conn.Close();
  55. if (image.Length == 0)
  56. return;
  57. string photoUrl = Environment.CurrentDirectory + "\\1.jpg";
  58. FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate, FileAccess.Write);
  59. BinaryWriter bw = new BinaryWriter(fs);
  60. bw.BaseStream.Write(image, 0, image.Length);
  61. bw.Flush();
  62. bw.Close();
  63. picbox_image.ImageLocation = photoUrl;
  64. }
  65. }
  66. }

效果如图:

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

C#操作SQL Server中的Image类型数据的更多相关文章

  1. delphi 转换sql server 中的 bit类型

    FieldByName('e').AsBoolean = false 其中e为 sql server 中的bit类型.

  2. 快速查看SQL Server 中各表的数据量以及占用空间大小

    快速查看SQL Server 中各表的数据量以及占用空间大小. CREATE TABLE #T (NAME nvarchar(100),ROWS char(20),reserved varchar(1 ...

  3. 浅析SQL Server 中的SOS_SCHEDULER_YIELD类型的等待

    本文出处:http://www.cnblogs.com/wy123/p/6856802.html 进程的状态转换 在说明SOS_SCHEDULER_YIELD等待之前,先简要介绍一下进程的状态(迷迷糊 ...

  4. SQL Server中时间段查询和数据类型转换

    不知道什么时候对数据独有情种,也许是因为所学专业的缘故,也许是在多年的工作中的亲身经历,无数据,很多事情干不了,数据精度不够,也很多事情干不了,有一次跟一个朋友开玩笑说,如果在写论文的时候,能有一份独 ...

  5. 【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

    从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件, 需要用不同的驱动程序来实现. 在64位的机 ...

  6. 【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据

    从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件,需要用不同的驱动程序来实现. 在32位的机器 ...

  7. 将SQL SERVER中查询到的数据导成一个Excel文件

    -- ====================================================== T-SQL代码: EXEC master..xp_cmdshell 'bcp 库名. ...

  8. 向SQL Server中导入Excel的数据

    1.  手动界面导入Excel数据 同 https://jingyan.baidu.com/article/ce09321b9a0e252bff858ff9.html 首先打开并登陆sql serve ...

  9. SQL Server中的uniqueidentifier类型

    uniqueidentifier类型可以配合T-SQL中的newid和newsequentialid来生成唯一标识符,具体区别如下(摘抄自微软官方文档). Nonsequential GUIDs: Y ...

随机推荐

  1. string去空格

    众所周知,string字符串去除空格的方法有trim()和replace(),区别在于trim()去首尾的空格,但是不能去中间的,而replace可以去除所有的空格. string data1=&qu ...

  2. css选择器参考手册

    选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id="firstna ...

  3. CentOS7时间设置及ntp同步配置(转)

    出处:http://www.centoscn.com/CentOS/config/2015/1105/6385.html http://www.centoscn.com/CentOS/config/2 ...

  4. Android——4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)

    Android的RIL机制中的 reference-ril.c 即为厂商提供的驱动接口.这个驱动源代码各个厂商都是有提供的,网上也有下载.我如今用的就是huawei wcdma的.最后编译成libre ...

  5. 说明sizeof和strlen之间的区别。

    解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...

  6. 【BZOJ4264】小C找朋友 随机化

    [BZOJ4264]小C找朋友 Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不 ...

  7. 【BZOJ1146】[CTSC2008]网络管理Network 树状数组+DFS序+主席树

    [BZOJ1146][CTSC2008]网络管理Network Description M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工 ...

  8. EasyNVR实现海康、大华NVR硬盘录像机Web无插件播放方案(支持取特定时间段视频流)

    本文转自:https://blog.csdn.net/black_3717/article/details/79872725 背景说明: 由于视频自身的直观性和便利性,对于传统安防行业,摄像机的直播和 ...

  9. jquery获取form表单中的内容,并将表单内容更新到datagrid的一行

    //执行不刷新页面更新所修改的行 var arr = $('#patient_form').serializeArray();//将表单中的数据格式化成数组 var m = new Array(); ...

  10. 【题解】[CF718C Sasha and Array]

    [题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...