2014-02-25 5 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킬로바이트이지만, 바이트 배열 파일이 비어 ....

+0

귀하의 방법은 지나치게 많이하고 있습니다. 이것을 가장 작은 재현 가능 섹션으로 줄이기 위해 작은 메소드 (로컬 변수로 전환)로 분리하여 시작하십시오. 독자로부터 아무것도 반환되지 않았습니까? 파일에 _anything_을 쓸 수 있습니까? –

+0

표시가 반환됩니다. 나는 문제가 데이터베이스에 서면으로 있다고 생각한다. 나는 잘못된 것을하고 있는가? –

+0

나는 잘 모르겠다. 내가 말했던 것과는 다른 것들을 깨뜨리고 실제로 어떤 명령이 실행되는지 조사한다. 아마도 명령 텍스트를 첨부하기 전에 매개 변수를 추가하는 것이 문제일까요? 최소한 파일의 내용에 데이터베이스의 텍스트가 포함되어 있지 않은 경우 (메모장이나 16 진 편집기에서 열 때) 출력 파일에 쓰는 것이 명백한 문제입니다. –

답변

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

시계 작업 - 뮤즈, 수정 해 주셔서 감사합니다. –

관련 문제