2015-01-13 2 views
0

저장 프로 시저를 실행하고 해당 저장 프로 시저의 값을 반환하는 메서드가 있습니다. SQL에서 VarBinary 유형의 VIN이 있습니다. 나는 값을 얻기 위해 무엇을 사용할 수 있는지 잘 모르겠습니다. 이 반환 가정되는 값은 그러나 44을 반환C#의 GetBytes가 올바른 값을 반환하지 않습니다.

 VIN =result.GetBytes(3,0,outByte,0,bufferSize) 

:

0x00D1CCE9771AE7554D479F7B93A45611010000004158D130E5097EF2924DEC4C6255E5BAF4C8EF4C2AC2A8FD9F29295F41DA3550123C6C4575788F5E6

// Get Customer Product By CustomerID 
    public Model.CustomerProduct Get_CustomerProduct(int Customer_ID) 
    { 
     Model.CustomerProduct model = null; 

     string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString(); 
     SqlDatabase db = new SqlDatabase(myConnection); 
     int bufferSize = 100;     

     byte[] outByte = new byte[bufferSize]; 
     using (DbCommand command = db.GetStoredProcCommand("Get_Customer_Product")) 
     { 
      db.AddInParameter(command, "Customer_ID", DbType.Int32, Customer_ID); 

      var result = db.ExecuteReader(command); 

      try 
      { 
       if (result.FieldCount == 0) 
        model = null; 
       else 
       { 
        result.Read(); 
        model = new Model.CustomerProduct() 
        { 
         Product_Name = result.GetString(2) 
         ,VIN =result.GetBytes(3,0,outByte,0,bufferSize) // this return me wrong 



        }; 
       } 
      } 
      catch (Exception ex) 
      { 


      } 
      return model; 
     } 
    } 

내 문제는이 라인 :

내 코드입니다

답변

2

GetBytes 메서드는 배열 자체에 쓰지 않은 바이트 수를 반환합니다. outByte의 내용을 살펴보고 거기에 데이터를 찾아야합니다.

먼저 null 버퍼로 GetBytes을 호출하는 것이 좋습니다. 이것은 제대로 크기로 버퍼를 수는 필드의 길이를 반환하게됩니다 : 당신이 가지고있는 코드는 이제 실행하는 경우

int len = result.GetBytes(3, 0, null, 0, 0); 
byte[] buf = new byte[len]; 
result.GetBytes(3, 0, buf, 0, buf.Length); 

model = new Model.CustomerProduct() 
{ 
    Product_Name = result.GetString(2), 
    VIN = buf 
}; 

, 당신이 가장 가능성이 너무 byte[]VIN의 유형을 변경해야합니다

.

+0

감사합니다.하지만 모든 내용은 00000000000000000000000000000000000000000000000000000000입니다. – Alma

+0

db의 필드 유형은 무엇입니까? – Chris

+0

db 형식이 VarBinary (500) – Alma

2

GetBytes는 읽은 바이트 수를 반환합니다. Getbytes 반환 값을 VIN에 할당하므로 VIN에서 읽은 바이트 수가 표시됩니다.

대신 출력 버퍼에서 읽어야합니다. 즉, outByte에 읽은 바이트가 있습니다.

관련 문제