2010-02-13 35 views
7

Tôi đang sử dụng DocX để tạo các tệp .docx. Thay vì lưu trữ chúng trên ổ đĩa cứng, Chia sẻ hoặc SharePoint tôi muốn lưu trữ chúng bên trong Cơ sở dữ liệu SQL. Vì vậy, câu hỏi của tôi là:Tải lên/Tải xuống tệp từ SQL Server 2005/2008 từ ứng dụng WinForms C#?

  1. Làm cách nào để viết mã thích hợp để lưu tệp vào cơ sở dữ liệu đó?
  2. Cách viết mã thích hợp để truy xuất lại tệp vào cơ sở dữ liệu đó?
  3. Tôi nên đặt loại datatype nào cho một bảng để giữ các tệp docx? Nó là hình ảnh?

Với regards,

MadBoy

PS. Tôi thích mã thích hợp hơn về cách sử dụng Sử dụng cách 'trường học cũ':

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

Trả lời

6

Một tập tin docx giống như một file zip đó là bộ sưu tập của một số tập tin. Còn về chuyển đổi thành nhị phân và sau đó lưu vào DB

Something như followswill làm việc

Để lưu

 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(); 

Retrieval là một quá trình phức tạp chút như sau

  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(); 

Xem các bài viết sau để được trợ giúp và chi tiết

Tiết kiệm:

retreival:

+0

Cảm ơn điều này dường như là một. Tôi sẽ chờ đợi một chút nếu ai đó có một số ý tưởng khác, cách :-) Nếu không tôi sẽ chấp nhận nó như là câu trả lời cuối cùng. – MadBoy

+0

Vui mừng điều này đã giúp –

+0

Bất kỳ ý tưởng nào tại sao quá trình này quá chậm? –

2

Tôi biết nó không sử dụng độc giả, nhưng đây là mẫu để truy vấn và lưu vào đĩa. Để đặt nó trở lại nó sẽ chỉ là một chèn hoặc cập nhật đi qua trong một byte []. Kiểu dữ liệu sẽ là varbinary max trong sql.

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(); 

đọc có thể là một cái gì đó như thế này

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

Thần Tôi sẽ thử nó. – MadBoy

Các vấn đề liên quan