SQL 数据库中的 BLOB 文件作为块
我尝试修改示例: 链接到示例 但我收到错误;无法将“System.DBNull”类型的对象转换为“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;
}
}
I tried to modify the example from: link to example but i receive an error;Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
I suppose that the returned ID (UniqueIdentifier) isn't correct.
My code:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如另一个答案中所述,该示例已过时;我不建议使用它。
如果您打算将其作为练习,请更改 SQL 以将您创建的 ID 插入到
myTable
中,如下所示:As noted in the other answer, the example is obsolete; I would not recommend using it.
If you are set on making it work just as an exercise, change your SQL to insert the ID that you created into
myTable
, as follows:那个例子已经过时了。 SQL Server 2005 之后强烈建议不要使用
TEXTPTR
以及已弃用的 TEXT、NTEXT 和 IMAGE 类型。 SQL Server 2005 及更高版本中有效操作 BLOB 的正确方法是使用 UPDATE .WRITE 语法和 MAX 数据类型。如果您想查看示例,请查看 通过 ASP.Net MVC 从 SQL Server 下载和上传图像That example is obsolete. USe of
TEXTPTR
is strongly discouraged after SQL Server 2005, along with the deprecated TEXT, NTEXT and IMAGE types. The correct SQL Server 2005 and after method of efficiently manipulating BLOBs is to useUPDATE .WRITE
syntax and MAX data types. If you want to see an example look at Download and Upload images from SQL Server via ASP.Net MVC我找到了一个更好的方法 这里,这是 SQL Server 2005+ 的做法。
BinaryData 是表名称。
Data.Write 是系统函数调用,其中 Data 是列名称
I found a much better way here, this is the SQL Server 2005 + way todo it.
BinaryData is the table name.
Data.Write is a system function call where Data is a column name