gpt4 book ai didi

c# - SQL 数据库中的 BLOB 文件作为 block

转载 作者:太空狗 更新时间:2023-10-30 01:24:28 26 4
gpt4 key购买 nike

我试图修改示例来自:link to example但我收到一个错误;
无法将“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;
}
}

最佳答案

如另一个答案中所述,该示例已过时;我不推荐使用它。

如果您打算让它像练习一样工作,请更改您的 SQL 以将您创建的 ID 插入到 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);

关于c# - SQL 数据库中的 BLOB 文件作为 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9200675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com