2012-02-08 33 views
4

我試圖從修改例如:link to example但我收到一個錯誤;
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
我想返回的ID(唯一標識符)是不正確的。

我的代碼:

BLOB文件

public static Guid AddRecord(string firstCol, DateTime SecCol, string photoFilePath) 
{ 
    using (SqlConnection connection = new SqlConnection(
     "Data Source=(local);Integrated Security=true;Initial Catalog=Test;")) 
    { 
     SqlCommand addRec = new SqlCommand(
      "INSERT INTO myTable (firstCol,SecCol,Image) " + 
      "VALUES (@firstCol,@SecCol,0x0)" + 
      "SELECT @Identity = NEWID();" + 
      "SELECT @Pointer = TEXTPTR(Image) FROM myTable WHERE ID = @Identity", connection); 

     addRec.Parameters.Add("@firstCol", SqlDbType.VarChar, 25).Value = firstCol; 
     addRec.Parameters.Add("@SecCol", SqlDbType.DateTime).Value = SecCol; 

     SqlParameter idParm = addRec.Parameters.Add("@Identity", SqlDbType.UniqueIdentifier); 
     idParm.Direction = ParameterDirection.Output; 

     SqlParameter ptrParm = addRec.Parameters.Add("@Pointer", SqlDbType.Binary, 16); 
     ptrParm.Direction = ParameterDirection.Output; 

     connection.Open(); 

     addRec.ExecuteNonQuery(); 

     Guid newRecID = (Guid)idParm.Value; 

     StorePhoto(photoFilePath, (byte[])ptrParm.Value, connection); 

     return newRecID; 
    } 
} 
+0

@Mithrandir:THX。我無法在我的問題中正確格式化代碼。 – 2012-02-08 20:17:25

回答

2

如在其它應答指出的,例如是過時;我不會推薦使用它。

如果您正在使它的工作只是作爲一個練習,改變你的SQL插入你到myTable創建的,將按照如下設置:

SqlCommand addRec = new SqlCommand(
      "SELECT @Identity = NEWID();" + 
      "INSERT INTO myTable (ID,firstCol,SecCol,Image) " + 
      "VALUES (@Identity,@firstCol,@SecCol,0x0)" + 
      "SELECT @Pointer = TEXTPTR(Image) FROM myTable WHERE ID = @Identity", connection); 
+0

作品豐富。但是,如果它可以做得更好,我的'Windows窗體應用程序'。請指教我。 – 2012-02-08 20:35:59

+0

@Robertico的'TEXTPTR'功能[在它的出路(http://msdn.microsoft.com/en-us/library/ms176068.aspx),所以我認爲更好的方式來切片圖像並加載它將是使用['SqlParameter'](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx)的['Offset'](HTTP:// MSDN。 microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.offset.aspx)屬性。不幸的是,微軟並沒有提供一個很好的例子,所以你需要四處搜索以獲得一個良好的演練。 – dasblinkenlight 2012-02-08 20:56:43

+0

感謝您指點我正確的方向。 – 2012-02-10 15:26:07

1

這個例子是過時的。在SQL Server 2005以及不推薦使用的TEXT,NTEXT和IMAGE類型之後,強烈建議不要使用TEXTPTR。正確的SQL Server 2005以及高效處理BLOB後的方法是使用UPDATE .WRITE語法和MAX數據類型。如果你想看一個例子看看Download and Upload images from SQL Server via ASP.Net MVC

+0

我的應用程序。是一個'Windows窗體應用程序'和服務器SQL服務器2008 R2。數據庫列類型是'圖像'。這是不可能改變的類型。 – 2012-02-08 20:27:00

0

我發現了一個好得多的方法here,這是SQL Server 2005 +的方式todo它。

string sql = "UPDATE BinaryData SET Data.Write(@data, LEN(data), @length) WHERE [email protected]"; 

     SqlParameter dataParam = cmd.Parameters.Add("@data", SqlDbType.VarBinary); 
     SqlParameter lengthParam = cmd.Parameters.Add("@length", SqlDbType.Int); 
     cmd.CommandText = sql; 

     fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
     int readBytes = 0; 
     while (cIndex < fileSize) 
     { 
      if (cIndex + BUFFER_SIZE > fileSize) 
       readBytes = fileSize - cIndex; 
      else 
       readBytes = BUFFER_SIZE; 
      fs.Read(buffer, 0, readBytes); 

      dataParam.Value = buffer; 
      dataParam.Size = readBytes; 
      lengthParam.Value = readBytes; 

      cmd.ExecuteNonQuery(); 
      cIndex += BUFFER_SIZE; 
     } 

BinaryData是表名。

Data.Write是一個系統函數調用,其中數據是列名