日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SQLServer2008中使用FileStream存取大文件實(shí)例介紹

SQL Server 2008中使用FileStream存取大文件的相關(guān)知識(shí)是本文我們主要要介紹的內(nèi)容,我們知道,SQL Server 2008中引入了Filestream,使用它可以將非機(jī)構(gòu)化大型數(shù)據(jù)(如文本文檔、圖像和視頻)等以varbinary(max)的形式存儲(chǔ)在文件系統(tǒng)中。使用數(shù)據(jù)庫的備份還原功能可以將這些數(shù)據(jù)一起備份還原。本文將簡單總結(jié)如何創(chuàng)建可以使用FileStream的數(shù)據(jù)庫以及如何使用c#訪問存取數(shù)據(jù)。

1. 創(chuàng)建數(shù)據(jù)庫

創(chuàng)建數(shù)據(jù)時(shí)可以執(zhí)行以下T-SQL語句,

 
 
 
  1. CREATE DATABASE TestDB     
  2. ON    
  3. PRIMARY ( NAME = TestDB,    
  4. FILENAME = 'c:\data\TestDB.mdf'),    
  5. FILEGROUP FileStreamGroup1 CONTAINS FILESTREAM( NAME = Arch3,    
  6. FILENAME = 'c:\data\filestream1')    
  7. LOG ON  ( NAME = TestDBlog,    
  8. FILENAME = 'c:\data\TestDBlog.ldf')    
  9. GO   

如果是在已經(jīng)創(chuàng)建好的數(shù)據(jù)庫上啟用FileStream,可以

 
 
 
  1. a. Right click the “TestDB” database and select “Properties”.  
  2. b. First create a FileGroup, click “Filegroups” menu and create one under “Stream” section,   
  3. named “FileStreamGroup1”. And then click “Files” menu, and add a file named “filestream1”   
  4. and Set it’s FileGroup to “FileStreamGroup1”, and then specify a folder to hold data,   
  5. like c:\Data.  Click ok, and you can check the folder “c:\data” to see what is added. 

2. 創(chuàng)建表

 
 
 
  1. CREATE TABLE [dbo].[TestTable2](    
  2. [ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,    
  3. [ContentType] [nvarchar](50) NOT NULL,    
  4. [FileName] [nvarchar](50) NOT NULL,    
  5. [FileContent] [varbinary](max) FILESTREAM  NULL,    
  6. [FileSize] [int] NULL,    
  7. CONSTRAINT [PK_TestTable2] PRIMARY KEY CLUSTERED     
  8. (    
  9. [ID] ASC    
  10. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,  
  11. ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] FILESTREAM_ON [FileStreamGroup1]    
  12. ) ON [PRIMARY] FILESTREAM_ON [FileStreamGroup1]    
  13. GO    
  14. SET ANSI_PADDING OFF    
  15. GO 

如果表已經(jīng)創(chuàng)建,可以執(zhí)行以下TSQL來設(shè)置:

 
 
 
  1. ALTER TABLE [dbo].[TestTable] alter column ID [uniqueidentifier] not null  
  2. ALTER TABLE [dbo].[TestTable] alter column ID add ROWGUIDCOL  
  3. ALTER TABLE [dbo].[TestTable] add FileContent varbinary(MAX) FILESTREAM; 

3. 使用C#讀寫

讀:

 
 
 
  1. SqlConnection conn = null;conn = new SqlConnection(connect);                    
  2. conn.Open();    
  3. tx = conn.BeginTransaction();var qry = "SELECT FileName, FileContent.PathName() as FilePath,  
  4. ContentType, GET_FILESTREAM_TRANSACTION_CONTEXT() as TranContext  FROM TestTable2 WHERE ID = @ID";    
  5. var cmd = new SqlCommand(qry, conn, tx);    
  6. cmd.Parameters.AddWithValue("@ID", id);using (rdr = cmd.ExecuteReader())    
  7. {     if (rdr.HasRows)    
  8. {    
  9. rdr.Read();    
  10. fileModel = new FileModel();    
  11. fileModel.FileName = Convert.ToString(rdr["FileName"]);    
  12. fileModel.FilePath = Convert.ToString(rdr["FilePath"]);    
  13. byte[] tranContext = (byte[])rdr["TranContext"];    
  14. fileModel.ContentType = Convert.ToString(rdr["ContentType"]); ;    
  15. fileModel.FileStream = new SqlFileStream(fileModel.FilePath, tranContext, FileAccess.Read);    
  16. }    
  17. }  

得到文件的Stream,就可以對(duì)該文件進(jìn)行讀寫操作了。

寫:

 
 
 
  1. public static void PostFileToDB(HttpPostedFileBase file)    
  2. {    
  3. string fileName = Path.GetFileName(file.FileName);    
  4. string contentType = file.ContentType;    
  5. int filefileSize = file.ContentLength / 1024;    
  6. using (SqlConnection conn = new SqlConnection(connect))    
  7. {    
  8. conn.Open();    
  9. using (SqlTransaction trn = conn.BeginTransaction ())    
  10. {    
  11. SqlCommand cmdInsert = new SqlCommand(    
  12. @"insert into TestTable2    
  13. (FileName, FileContent, ContentType, FileSize)       
  14. output    
  15. INSERTED.FileContent.PathName(),    
  16. GET_FILESTREAM_TRANSACTION_CONTEXT ()                                            
  17. values    
  18. (@FileName, 0x, @ContentType, @FileSize)", conn, trn);    
  19. cmdInsert.Parameters.Add("@FileName", SqlDbType.VarChar, 256);    
  20. cmdInsert.Parameters["@FileName"].Value = fileName;    
  21. cmdInsert.Parameters.Add("@ContentType", SqlDbType.VarChar, 256);    
  22. cmdInsert.Parameters["@ContentType"].Value = contentType;     
  23. cmdInsert.Parameters.Add("@FileSize", SqlDbType.Int);    
  24. cmdInsert.Parameters["@FileSize"].Value = fileSize;    
  25. string path = null;    
  26. byte[] context = null;                  
  27. // cmdInsert is an INSERT command that uses the OUTPUT clause     
  28. // Thus we use the ExecuteReader to get the     
  29. // result set from the output columns     
  30. // using (SqlDataReader rdr = cmdInsert.ExecuteReader())    
  31. {    
  32. rdr.Read();    
  33. path = rdr.GetString(0);    
  34. context = rdr.GetSqlBytes(1).Buffer;    
  35. }                      
  36. using (SqlFileStream sfs = new SqlFileStream(    
  37. path, context, FileAccess.Write))    
  38. {    
  39. file.InputStream.CopyTo(sfs);    
  40. }    
  41. trn.Commit ();    
  42. }    
  43. }    
  44. }  

關(guān)于SQL Server 2008中使用FileStream存取大文件實(shí)例就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. 初學(xué)SQL Server數(shù)據(jù)庫的一些常用操作總結(jié)
  2. SQL Server數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)倉庫已分區(qū)表詳解
  3. SQL Server與Access數(shù)據(jù)庫ASP代碼的比較詳解
  4. SQL Server數(shù)據(jù)庫中bit字段類型使用時(shí)的注意事項(xiàng)
  5. SQL Server數(shù)據(jù)庫timestamp數(shù)據(jù)類型相關(guān)知識(shí)介紹

本文標(biāo)題:SQLServer2008中使用FileStream存取大文件實(shí)例介紹
網(wǎng)頁路徑:http://m.5511xx.com/article/cohcpic.html