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

C# SQL数据库中的BLOB文件作为块,c#,sql-server,blob,uniqueidentifier,chunks,C#,Sql Server,Blob,Uniqueidentifier,Chunks,我试图修改的例子:但我收到一个错误无法将“System.DBNull”类型的对象强制转换为“System.Byte[]”类型。我想返回的ID(UniqueIdentifier)不正确。我的代码: public static Guid AddRecord(string firstCol, DateTime SecCol, string photoFilePath) { using (SqlConnection connection = new SqlConnection(

我试图修改的例子:但我收到一个错误
无法将“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);

这个例子已经过时了。SQL Server 2005之后,强烈反对使用
textpr
,以及不推荐使用的文本、NTEXT和图像类型。有效操作blob的正确SQL Server 2005和after方法是使用
UPDATE.WRITE
语法和MAX数据类型。如果您想看一个示例,请看

我找到了一个更好的方法,这是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;
        }
string sql=“更新二进制数据集数据。写入(@Data,LEN(Data),@length),其中fileName=@fileName”;
SqlParameter dataParam=cmd.Parameters.Add(“@data”,SqlDbType.VarBinary);
SqlParameter lengthParam=cmd.Parameters.Add(“@length”,SqlDbType.Int);
cmd.CommandText=sql;
fs=新文件流(文件名,FileMode.Open,FileAccess.Read);
int readBytes=0;
而(cIndex<文件大小)
{
如果(cIndex+缓冲区大小>文件大小)
readBytes=fileSize-cIndex;
其他的
readBytes=缓冲区大小;
fs.Read(缓冲区,0,readBytes);
数据参数值=缓冲区;
dataParam.Size=readBytes;
lengthParam.Value=readBytes;
cmd.ExecuteNonQuery();
cIndex+=缓冲区大小;
}
BinaryData是表名


Data.Write是一个系统函数调用,其中Data是一个列名

@Mithrandir:Thx。我的问题中的代码格式不正确。我的应用程序。是一个“Windows窗体应用程序”和服务器SQL server 2008 R2。数据库列类型为“Image”。无法更改类型。工作异常。但如果我的“Windows窗体应用程序”可以做得更好的话。请给我建议。@Robertico使用
TEXTPTR
功能,因此我认为更好的分割图像并加载图像的方法是使用的属性。不幸的是,微软并没有提供一个很好的例子,所以你需要四处寻找一个很好的例子。