C#從SQL Server中讀寫大資料列
/*
Author:Wu Xiuxiang;
Email:imessage@126.com
*/
public static void Main()
{
//寫入大物件到SqlServer
FileStream fs = new FileStream("C:test.bmp",FileMode.OPen,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=northwind");
string cmdText = "UPDATE EMPLOYEES" +
"SET Photo=@image where EmployeeId=1";
SqlCommand cmd = new SqlCommand(cmdText,conn);
cmd.Parameters.Add("@image",SqlDbType.Image);
cmd.Parameters["@image"].Value = br.ReadBytes((int)br.BaseStream.Length);
conn.Open();
int i=cmd.ExecuteNoQuery();
//從SQL Server中讀取大物件 string cmdtext = "SELECT employeeid,photo" +
" from employees where employeeid = 1";
SqlCommand cmd2 = new SqlCommand(cmdtext,conn);
FileStream rfs;
BinaryWriter rbw;
long numread;
long startIndex;
int buffSize=4096;
byte[] buff = new byte[buffSize];
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
if(rdr.Read())
{
int empid = rdr.GetInt32(0);
fs = new FileStream("c:mypic.bmp",FileMode.OpenOrCreate,FileAccess.Write);
bw = new BinaryWrite(fs);
startIndex=0;
numread = rdr.GetBytes(1,startIndex,buff,0,buffSize);
while(numread==buffSize)
{
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();
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/631872/viewspace-862717/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Logstash : 從 SQL Server 讀取資料SQLServer
- 從舊版本SQL Server中重新儲存資料SQLServer
- c#連線SQL Server資料庫C#SQLServer資料庫
- java poi讀取Excel資料 插入到SQL SERVER資料庫中JavaExcelSQLServer資料庫
- Sql server:從XML檔案中讀取配置資訊SQLServerXML
- sql server:刪除表資料,標識列Id從1開始SQLServer
- 轉發:C#操作SQL Server資料庫C#SQLServer資料庫
- 資料讀寫壓力大,讀寫分離
- SQL Server AlwaysOn讀寫分離配置SQLServer
- 使用SQL-Server分割槽表功能提高資料庫的讀寫效能SQLServer資料庫
- Java學習筆記-----從套接字中讀寫資料Java筆記
- 小寫轉大寫金額[SQL SERVER] (轉)SQLServer
- 在SQL Server中謹慎匯入匯出大容量資料SQLServer
- C#快速入門教程(27)—— SQL Server資料庫C#SQLServer資料庫
- SQL Server資料庫基礎之行資料轉換為列資料SQLServer資料庫
- SQL Server中獲取一個資料庫的所有表的列數SQLServer資料庫
- intel realsense usb列舉,讀寫資料解析Intel
- SQL Server 中自定義資料型別SQLServer資料型別
- SQL Server中操作XML型別資料SQLServerXML型別
- SQL Server 資料庫開發中的十大問題VYSQLServer資料庫
- 在VC中用OLE DB讀寫SQL Server中的BLOB欄位 (轉)SQLServer
- SQL SERVER(openrowset函式)從檔案(.dbf)讀取資料並將資料作為行集返回SQLServer函式
- SQL Server 中讀取當前年月SQLServer
- SQL SERVER與C#的資料型別對應表SQLServerC#資料型別
- C# 移除陣列中重複資料C#陣列
- 用SQL Server 資料庫應該如何寫 ?? 謝謝!SQLServer資料庫
- Sql Server資料庫資料匯入到SQLite資料庫中Server資料庫SQLite
- 在SQL Server資料庫中匯入匯出資料SQLServer資料庫
- 在SQL Server資料庫中匯入MySQL資料庫Server資料庫MySql
- Sql Server 資料庫獲取字串中小寫字母的SQL語句SQLServer資料庫字串
- SQL Server資料庫對大容量表的操作SQLServer資料庫
- SQL Server 中的25種資料型別SQLServer資料型別
- SQL Server中易混淆的資料型別SQLServer資料型別
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- C# winform中讀寫ini檔案C#ORM
- C#中讀寫INI配置檔案C#
- 大資料系列2:Hdfs的讀寫操作大資料
- SQL server中的日期變數縮寫SQLServer變數