2010-02-13 27 views
7

我使用DocX創建.docx文件。而不是將它們存儲在硬盤驅動器上,我寧願將它們存儲在SQL數據庫中。所以我的問題是:從WinForms C#應用程序的SQL Server 2005/2008上傳/下載文件?

  1. 如何編寫正確的代碼來保存文件到該數據庫?
  2. 如何編寫正確的代碼來檢索文件到該數據庫?
  3. 我應該爲表格設置哪種數據類型來保存docx文件?它是圖像嗎?

與問候,

MadBoy

PS。我會使用舊的「學校」的方式使用方面更喜歡正確代碼:

using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetailsDepozyt)) 
using (var sqlQuery = new SqlCommand(preparedCommand, varConnection)) 
using (var sqlQueryResult = sqlQuery.ExecuteReader()) 
    while (sqlQueryResult != null && sqlQueryResult.Read()) 

回答

6

一個的docx文件就像是一個zip文件是幾個文件的集合。怎麼樣轉換成二進制文件,然後保存到數據庫

東西作爲followswill工作

爲了節省

 string filePath = ""; 
     string connectionString = ""; 
     FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
     BinaryReader reader = new BinaryReader(stream); 
     byte[] file = reader.ReadBytes((int)stream.Length); 
     reader.Close(); 
     stream.Close(); 

     SqlCommand command; 
     SqlConnection connection = new SqlConnection(connectionString); 
     command = new SqlCommand("INSERT INTO FileTable (File) Values(@File)", connection); 
     command.Parameters.Add("@File", SqlDbType.Binary, file.Length).Value = file; 
     connection.Open(); 
     command.ExecuteNonQuery(); 

檢索是有點複雜的過程如下

  SqlConnection connection = new SqlConnection(""); 
      string query = 
      @" SELECT File FROM FileTable where FileID =" + 125; 
      SqlCommand command = new SqlCommand(query, connection); 

      FileStream stream; 
      BinaryWriter writer; 

      int bufferSize = 100; 
      byte[] outByte = new byte[bufferSize]; 

      long retval; 
      long startIndex = 0; 

      string pubID = ""; 

      connection.Open(); 
      SqlDataReader reader = command.ExecuteReader(CommandBehavior.Default); 

      while (reader.Read()) 
      {  
       pubID = reader.GetString(0); 

       stream = 
       new FileStream("abc.docx", FileMode.OpenOrCreate, FileAccess.Write); 
       writer = new BinaryWriter(stream); 
       startIndex = 0; 
       retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize); 

       while (retval == bufferSize) 
       { 
        writer.Write(outByte); 
        writer.Flush(); 
        startIndex += bufferSize; 
        retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize); 
       } 
       writer.Write(outByte, 0, (int)retval - 1); 
       writer.Flush(); 
       writer.Close(); 
       stream.Close(); 
      } reader.Close(); 
      connection.Close(); 

請看以下文章以獲取幫助和詳細信息

保存

Retreival

+0

感謝這似乎是一個。如果別人有更多的想法,我會稍微等一下:-)如果不是,我會接受它作爲最終答案。 – MadBoy 2010-02-14 18:52:06

+0

很高興幫助 – 2010-02-14 19:28:02

+0

任何想法爲什麼這個過程如此緩慢? – 2012-04-24 19:54:29

2

我知道這是不使用閱讀器,但這裏是查詢並保存到磁盤的樣本。把它放回來,它只是一個插入或更新傳遞一個字節[]。數據類型是sql中的varbinary max。

SqlConnection connection = new SqlConnection ("..."); 
    connection.Open(); 
    SqlCommand command = new 
     SqlCommand ("select...e", connection); 
    byte[] buffer = (byte[]) command.ExecuteScalar(); 
    connection.Close(); 
    FileStream fs = new FileStream(@"C:\test.pdf", FileMode.Create); 
    fs.Write(buffer, 0, buffer.Length); 
    fs.Close(); 

讀者可能會是這樣的

while (myReader.Read()) 
     { 
      byte[] file = myReader.GetBytes(0)); 
      // might be 
      file = (byte[])GetValue(0); 
     } 
+0

比ks我會試試看。 – MadBoy 2010-02-14 18:52:21

相關問題