我试图修改示例:链接到示例,但收到错误消息;Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
我想返回的 ID (UniqueIdentifier) 不正确。
我的代码:
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;
}
}