2014-02-25 64 views
1

我試圖用blob colomun將文件上傳到表中。它的工作,但不正確,可以幫助嗎? 此代碼上傳,但不正確。當我打開表格時,我看到了這個| ID = 1 | [email protected] |Db2 blob上傳文件

FileStream stream = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read); 
      BinaryReader reader = new BinaryReader(stream); 
      byte[] file = reader.ReadBytes((int)stream.Length); 

      reader.Close(); 
      stream.Close(); 
      string query = "UPDATE UPDATE_Z SET FILE_Z=? WHERE ID='"+1+"';"; 

      { 
       OleDbConnection connection = new OleDbConnection(conn); 
       connection.Open(); 
       OleDbCommand command = connection.CreateCommand(); 
       // command.CreateParameter(); 

       command.CommandText = query; 

       command.Parameters.Add("FILE_Z", OleDbType.Binary, file.Length); 
       command.Parameters[0].Value = file; 
       command.ExecuteNonQuery(); 
       connection.Close(); 
       // for (int i=0; i < file.Length;i++) 
       //{ 
        // MessageBox.Show(file[i].ToString()); 
       //} 

      } 
      query = "SELECT FILE_Z FROM UPDATE_Z where ID ='"+1+"'"; 
      FileStream stream1; 
      BinaryWriter writer; 

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

      long retval; 
      long startIndex = 0; 

      string pubID = ""; 


      OleDbConnection connection1 = new OleDbConnection(conn); 
      connection1.Open(); 
      OleDbCommand command1 = connection1.CreateCommand(); 
      command1.CommandText = query; 
      OleDbDataReader reader1 = command1.ExecuteReader(System.Data.CommandBehavior.Default); 



      while (reader1.Read()) 
      { 
      // pubID = reader1.GetString(0); 

       stream1 = new FileStream("version1.txt", FileMode.OpenOrCreate, FileAccess.Write); 
       writer = new BinaryWriter(stream1); 
       startIndex = 0; 
       retval = reader1.GetBytes(0, startIndex, outByte, 0, bufferSize); 

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

       writer.Close(); 

       stream1.Close(); 
      } 

      reader1.Close(); 
      connection1.Close(); 

和輸出文件 「ZAP.EXE」 的大小爲0 KB,但字節數組文件不爲空....

+0

你的方法做得太多了。首先將其分解成更小的方法(切換到局部變量),試圖將其減少到最小的可再現部分。讀者沒有回報什麼?你能寫文件_anything_嗎? –

+0

注意到返回。我認爲問題在於寫入數據庫......我做錯了什麼? –

+0

我不知道......像我說的那樣把事情分開,並調查實際執行的命令。也許事實上,在附加命令文本之前添加一個參數是一個問題?至少,如果文件的內容甚至不包含數據庫的文本(在記事本或十六進制編輯器中打開它),那麼寫入輸出文件有一個明顯的問題。 –

回答

0

此代碼的工作對我來說:

FileStream stream = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read); 
      BinaryReader reader = new BinaryReader(stream); 
      byte[] file = reader.ReadBytes((int)stream.Length); 

      reader.Close(); 
      stream.Close(); 
      string query = "UPDATE UPDATE_Z SET FILE_Z=? WHERE ID='"+1+"';"; 

      { 
       OleDbConnection connection = new OleDbConnection(conn); 
       connection.Open(); 
       OleDbCommand command = connection.CreateCommand(); 
       // command.CreateParameter(); 

       command.CommandText = query; 

       command.Parameters.Add("FILE_Z", OleDbType.Binary, file.Length); 
       command.Parameters[0].Value = file; 
       command.ExecuteNonQuery(); 
       connection.Close(); 
       // for (int i=0; i < file.Length;i++) 
       //{ 
        // MessageBox.Show(file[i].ToString()); 
       //} 

      } 
      query = "SELECT FILE_Z FROM UPDATE_Z where ID ='"+1+"'"; 
      FileStream stream1; 
      BinaryWriter writer; 

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

      long retval; 
      long startIndex = 0; 

      string pubID = ""; 


      OleDbConnection connection1 = new OleDbConnection(conn); 
      connection1.Open(); 
      OleDbCommand command1 = connection1.CreateCommand(); 
      command1.CommandText = query; 
      OleDbDataReader reader1 = command1.ExecuteReader(System.Data.CommandBehavior.Default); 



      while (reader1.Read()) 
      { 
      // pubID = reader1.GetString(0); 

       stream1 = new FileStream("version1.txt", FileMode.OpenOrCreate, FileAccess.Write); 
       writer = new BinaryWriter(stream1); 
       startIndex = 0; 
       retval = reader1.GetBytes(0, startIndex, outByte, 0, bufferSize); 

       while (retval == bufferSize) 
       { 
        writer.Write(outByte); 
        writer.Flush(); 
        startIndex += bufferSize; 
        retval = reader1.GetBytes(0, startIndex, outByte, 0, bufferSize); 
       } 
       writer.Write(outByte, 0, (int)retval); 
       writer.Flush(); 

       writer.Close(); 

       stream1.Close(); 
      } 

      reader1.Close(); 
      connection1.Close(); 

問題是這個代碼writer.Write(outByte, 0, (int)retval - 1); 從文件丟失的最後一個字節和文件未滿,我不知道這行代碼需要什麼...

+0

雖然回答你自己的問題當然是可以接受的,但你至少應該解釋爲什麼_why_解決了這個問題。是什麼讓你的原代碼不正確? –

+0

Clockwork-Muse,感謝您的糾正。 –