-
从SQL Server中读写大数据列。
public static void Main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//写入大对象到SqlServer
FileStream fs = new FileStream("C:\\test.bmp",FileMode.OPen,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
![](/Images/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=northwind");
![](/Images/OutliningIndicators/InBlock.gif)
string cmdText = "UPDATE EMPLOYEES" +
"SET Photo=@image where EmployeeId=1";
SqlCommand cmd = new SqlCommand(cmdText,conn);
cmd.Parameters.Add("@image",SqlDbType.Image);
![](/Images/OutliningIndicators/InBlock.gif)
cmd.Parameters["@image"].Value = br.ReadBytes((int)br.BaseStream.Length);
![](/Images/OutliningIndicators/InBlock.gif)
conn.Open();
int i=cmd.ExecuteNoQuery();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//从SQL Server中读取大对象
string cmdtext = "SELECT employeeid,photo" +
" from employees where employeeid = 1";
SqlCommand cmd2 = new SqlCommand(cmdtext,conn);
![](/Images/OutliningIndicators/InBlock.gif)
FileStream rfs;
BinaryWriter rbw;
![](/Images/OutliningIndicators/InBlock.gif)
long numread;
long startIndex;
int buffSize=4096;
byte[] buff = new byte[buffSize];
![](/Images/OutliningIndicators/InBlock.gif)
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
if(rdr.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int empid = rdr.GetInt32(0);
![](/Images/OutliningIndicators/InBlock.gif)
fs = new FileStream("c:\\mypic.bmp",FileMode.OpenOrCreate,FileAccess.Write);
bw = new BinaryWrite(fs);
startIndex=0;
![](/Images/OutliningIndicators/InBlock.gif)
numread = rdr.GetBytes(1,startIndex,buff,0,buffSize);
![](/Images/OutliningIndicators/InBlock.gif)
while(numread==buffSize)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bw.Write(buff);
bw.Flush();
startIndex+=buffSize;
numread = rdr.GetBytes(1,startIndex,buff,buffSize);
}
bw.Write(buff);
bw.Flush();
bw.Close();
fs.Close();
}
rdr.Close();
conn.Close();
}
-
相关阅读:
zabbix--完整安装攻略
python--8大排序(原理+代码)
python--二分法查找
celery生产者-消费者
python--基础知识点梳理(之数据结构)
mysql--事务详解
python--基础知识点梳理(三)深浅拷贝、进线协程、os和sys、垃圾回收机制、读文件的三种方式
python--基础知识点梳理(二)面向对象
python--基础知识点梳理(一)数据类型、迭代生成装饰器、函数
java 集合
-
原文地址:https://www.cnblogs.com/wxx/p/188969.html
Copyright © 2020-2023
润新知