SQL SERVER 2008中使用VARBINARY(MAX)进行图像存取的实现方法
CREATE DATABASE ImageTest
GO
USE ImageTest;
GO
CREATE TABLE ImageTable
(
ImageData varBinary(MAX)
);
INSERT INTO ImageTable (ImageData)
SELECT *
FROM OPENROWSET(BULK N'C:\aa.jpg', SINGLE_BLOB) AS Photo
GO
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
EXEC master..xp_cmdshell 'bcp "SELECT ImageData FROM ImageTest.dbo.ImageTable" queryout "c:\bb.jpg"-T -c'
Go
public static bool StoreImages(string[] fileNames, string[] filePaths)
{
try
{
for (int i = 0; i < fileNames.Length; i++)
{
string fileName = fileNames[i];
string filePath = filePaths[i]; using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString))
{
connection.Open();
FileStream byteStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
byte[] byteImage = new byte[byteStream.Length];
byteStream.Read(byteImage, 0, (int)byteStream.Length);
string strSql = "insert into Data(Name,Data,DataPath) values(@Name,@Data,@DataPath)";
using (SqlCommand cmd = new SqlCommand(strSql, connection))
{
cmd.Parameters.Add("@Name", SqlDbType.Text);
cmd.Parameters.Add("@Data", SqlDbType.Binary);
cmd.Parameters.Add("@DataPath", SqlDbType.Text);
cmd.Parameters["@Name"].Value = fileName;
cmd.Parameters["@Data"].Value = byteImage;
cmd.Parameters["@DataPath"].Value = filePath;
cmd.ExecuteNonQuery();
}
}
} return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public static Image GetImageFromName(string fileName)
{
try
{
using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString))
{
connection.Open();
string strSql = "select Data from Data where Name = '" + fileName + "'";
SqlCommand cmd = new SqlCommand(strSql, connection);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
MemoryStream ms = new MemoryStream((byte[])dr[0]);
Image img = Image.FromStream(ms);
return img;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
SQL SERVER 2008中使用VARBINARY(MAX)进行图像存取的实现方法的更多相关文章
- SQL Server 2008中新增的 1.变更数据捕获(CDC) 和 2.更改跟踪
概述 1.变更数据捕获(CDC) 每一次的数据操作都会记录下来 2.更改跟踪 只会记录最新一条记录 以上两种的区别: http://blog.csdn.n ...
- SQL Server 2008中新增的变更数据捕获(CDC)和更改跟踪
来源:http://www.cnblogs.com/downmoon/archive/2012/04/10/2439462.html 本文主要介绍SQL Server中记录数据变更的四个方法:触发器 ...
- SQL点滴15—在SQL Server 2008中调用C#程序
原文:SQL点滴15-在SQL Server 2008中调用C#程序 T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可 ...
- 在SQL Server 2008中调用.net,dll
原文:在SQL Server 2008中调用.net,dll T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可以在SQ ...
- SQL Server 2008中的数据压缩
SQL Server 2008中引入了数据压缩的功能,允许在表.索引和分区中执行数据压缩.这样不仅可以大大节省磁盘的占用空间,还允许将更多数据页装入内存中,从而降低磁 盘IO,提升查询的性能.当然,凡 ...
- SQL Server 2008中增强的"汇总"技巧
本文转载:http://www.cnblogs.com/downmoon/archive/2012/04/06/2433988.html SQL Server 2008中的Pivot和UnPivot: ...
- SQL Server 2008中数据压缩
SQL Server 2008中引入了数据压缩的功能,允许在表.索引和分区中执行数据压缩.这样不仅可以大大节省磁盘的占用空间,还允许将更多数据页装入内存中,从而降低磁盘IO,提升查询的性能.当然,凡事 ...
- 利用Ring Buffer在SQL Server 2008中进行连接故障排除
原文:利用Ring Buffer在SQL Server 2008中进行连接故障排除 出自:http://blogs.msdn.com/b/apgcdsd/archive/2011/11/21/ring ...
- SQL Server 2008中的CDC(Change Data Capture)功能使用及释疑
SQL Server 2008中的CDC(Change Data Capture)功能使用及释疑 关键词:CDC 原文:http://www.cnblogs.com/chenxizhang/arc ...
随机推荐
- 通过 :hover 伪元素控制其他元素
---代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- requireJS 加载css、less文件
-- requireJS 同样可以加载css 文件,有require-css的插件,只需要把插件放入main.js同文件夹,在依赖处 采用 ‘css! test.css’的形式就可以加载css文件 - ...
- 在SQL SERVER 2008中通过已有的数据库生成建库脚本
- CNN中下一层Feature map大小计算
符号表示: $W$:表示当前层Feature map的大小. $K$:表示kernel的大小. $S$:表示Stride的大小. 具体来讲: 整体说来,和下一层Feature map大小最为密切的就是 ...
- 「LOJ#103」子串查找 (Hash
题目描述 这是一道模板题. 给定一个字符串 A A A 和一个字符串 B B B,求 B B B 在 A A A 中的出现次数.AAA 和 BBB 中的字符均为英语大写字母或小写字母. A A A 中 ...
- 「USACO16OPEN」「LuoguP3146」248(区间dp
题目描述 Bessie likes downloading games to play on her cell phone, even though she doesfind the small to ...
- caffe源码学习
本文转载自:https://buptldy.github.io/2016/10/09/2016-10-09-Caffe_Code/ Caffe简介 Caffe作为一个优秀的深度学习框架网上已经有很多内 ...
- 禁用ubuntu 客人会话
sudo vi /usr/share/lightdm/lightdm.conf.d/50-guest-wrapper.conf 添加: allow-guest=false 重启.
- Vue 项目中添加全局过滤器以及全局混合mixin
可以在.vue文件中定义局部使用的过滤器 export default{ data(){ return [] }, filters:{ toUpperCase:function(value){ ret ...
- _beginThreadex的用法
http://blog.csdn.net/cjcy1984001/article/details/6675669 线程开始和停止函数! unsigned long _beginthreadex( v ...