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
C#操作SQL Server中的Image类型数据的更多相关文章
- delphi 转换sql server 中的 bit类型
FieldByName('e').AsBoolean = false 其中e为 sql server 中的bit类型.
- 快速查看SQL Server 中各表的数据量以及占用空间大小
快速查看SQL Server 中各表的数据量以及占用空间大小. CREATE TABLE #T (NAME nvarchar(100),ROWS char(20),reserved varchar(1 ...
- 浅析SQL Server 中的SOS_SCHEDULER_YIELD类型的等待
本文出处:http://www.cnblogs.com/wy123/p/6856802.html 进程的状态转换 在说明SOS_SCHEDULER_YIELD等待之前,先简要介绍一下进程的状态(迷迷糊 ...
- SQL Server中时间段查询和数据类型转换
不知道什么时候对数据独有情种,也许是因为所学专业的缘故,也许是在多年的工作中的亲身经历,无数据,很多事情干不了,数据精度不够,也很多事情干不了,有一次跟一个朋友开玩笑说,如果在写论文的时候,能有一份独 ...
- 【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据
从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件, 需要用不同的驱动程序来实现. 在64位的机 ...
- 【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据
从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件,需要用不同的驱动程序来实现. 在32位的机器 ...
- 将SQL SERVER中查询到的数据导成一个Excel文件
-- ====================================================== T-SQL代码: EXEC master..xp_cmdshell 'bcp 库名. ...
- 向SQL Server中导入Excel的数据
1. 手动界面导入Excel数据 同 https://jingyan.baidu.com/article/ce09321b9a0e252bff858ff9.html 首先打开并登陆sql serve ...
- SQL Server中的uniqueidentifier类型
uniqueidentifier类型可以配合T-SQL中的newid和newsequentialid来生成唯一标识符,具体区别如下(摘抄自微软官方文档). Nonsequential GUIDs: Y ...
随机推荐
- maven项目The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
用Eclipse创建了一个maven web程序,使用tomcat8.5作为服务器,可以正常启动,但是却报如下错误 The superclass "javax.servlet.http.Ht ...
- Cesium--气泡弹窗
参考资料 首先感谢以下博主们的帮助,本人刚接触Cesium不久,无奈只能拾人牙慧了. 由于cesium没有自带的点击弹出气泡的功能,所以需要自己去开发一个这样的功能,网络上资源很多,看到基本思路都一致 ...
- hadoop 相关工具访问端口(转)
原文:http://www.tuicool.com/articles/BB3eArJ hadoop系统部署时用到不少端口.有的是Web UI所使用的,有的是内部通信所使用的,有的是监控所使用的.实际系 ...
- poj 3071 Football <DP>
链接:http://poj.org/problem?id=3071 题意: 有 2^n 支足球队,编号 1~2^n,现在给出每支球队打败其他球队的概率,问哪只球队取得冠军的概率最大? 思路: 设dp[ ...
- C#单元测试(转)
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...
- 不依赖外部js es 库 实现 点击内容 切换
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta http-equiv=&qu ...
- pjax + tp5,实现局部无刷新返回数据
文件1:\application\admin\controller\Setting.php 最后一句代码:要fetch原页面 <?php namespace app\admin\controll ...
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
今天实现了一个模拟碟片加载过程的小demo,在此展示一下.由于在公司,不好截取动态图片,因此就在这截取两张静态图片看看效果先. 下面简单的将代码列出来. setp1.准备两张用于旋转的图片,如下:lo ...
- pip安装时使用国内源加快下载速度
国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 h ...
- css 样式(checkbox开关、css按钮)
checkbox开关 css .iosCheck { /* Blue edition */ } .iosCheck input { display: none; } .iosCheck i { dis ...
