SQL 数据库中的 BLOB 文件作为块

发布于 2025-01-03 09:18:32 字数 1459 浏览 4 评论 0原文

我尝试修改示例: 链接到示例 但我收到错误;
无法将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2025-01-10 09:18:32

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

如果您打算将其作为练习,请更改 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);

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:

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);
淡淡離愁欲言轉身 2025-01-10 09:18:32

那个例子已经过时了。 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 use UPDATE .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

孤檠 2025-01-10 09:18:32

我找到了一个更好的方法 这里,这是 SQL Server 2005+ 的做法。

string sql = "UPDATE BinaryData SET Data.Write(@data, LEN(data), @length) WHERE fileName=@fileName";

        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 是系统函数调用,其中 Data 是列名称

I found a much better way here, this is the SQL Server 2005 + way todo it.

string sql = "UPDATE BinaryData SET Data.Write(@data, LEN(data), @length) WHERE fileName=@fileName";

        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 is the table name.

Data.Write is a system function call where Data is a column name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文