我可以使用流插入或SQL服务器(C#)更新行? [英] Can I use a stream to INSERT or UPDATE a row in SQL Server (C#)?

查看:189
本文介绍了我可以使用流插入或SQL服务器(C#)更新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个VARBINARY [MAX]列,我可以插入或更新到利用的System.IO.Stream派生的类型列?怎么样?

Suppose I have a VarBinary[MAX] column, can I insert or update into that column using a type derived from System.IO.Stream? How?

我认为我可以取得的只读的从这样的列使用流的 SqlDataReader的,调用 GetSqlBytes()对读者,得到了SqlBytes实例,然后引用的该财产。

I think that I can obtain a read-only stream from such a column using a SqlDataReader, calling GetSqlBytes() on the reader, getting the SqlBytes instance, and then referencing the Stream property on that.

我要的是相反的 - 我要为更新流或插入。

What I want is the converse - I want a stream for update or insert.

可能吗? (从C#...无需编写T-SQL?)

Possible? (from c#... Without writing T-SQL ?)

修改

我见过这样的代码:

    System.Data.SqlClient.SqlCommand _SqlCommand
        = new System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection);

    // Convert image to memory stream
    System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
    _Image.Save(_MemoryStream, _ImageFormat);

    // Add image as SQL parameter
    System.Data.SqlClient.SqlParameter _SqlParameter 
        = new System.Data.SqlClient.SqlParameter("@" + _ImageFieldName, SqlDbType.Image);

    _SqlParameter.Value = _MemoryStream.ToArray();
    _SqlCommand.Parameters.Add(_SqlParameter);

    // Executes a Transact-SQL statement against the connection 
    // and returns the number of rows affected.
    _SqlRetVal = _SqlCommand.ExecuteNonQuery();

    // Dispose command
    _SqlCommand.Dispose();
    _SqlCommand = null;  



...但我不想使用数组来指定值。这适用于小数据尺寸,但不作为尺寸变大。我想写成流。

...but I don't want to use an array to specify the value. That works for small data sizes, but not as sizes get larger. I want to write into a stream.

推荐答案

请参阅的 http://stackoverflow.com/questions/2101149/how-to-i-serialize- A-大图-的净对象为-A-SQL服务器的Blob无/ 2151491#2151491 一个例子,如何使用语义流写入到一个BLOB

See http://stackoverflow.com/questions/2101149/how-to-i-serialize-a-large-graph-of-net-object-into-a-sql-server-blob-without/2151491#2151491 for an example how to use streaming semantics to write into a BLOB

这篇关于我可以使用流插入或SQL服务器(C#)更新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆