日期:2014-05-20 浏览次数:21270 次
SqlConnection conn =null; SqlCommand cmd = null; SqlParameter param = null; FileStream fs = null; const string sConn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password"; try { conn = new SqlConnection(sConn); cmd = new SqlCommand("UPDATE Categories SET Picture = ?Picture WHERE CategoryName = 'Seafood'", conn); fs = new FileStream("c:\\Builder.doc", FileMode.Open, FileAccess.Read); Byte[] blob = new Byte[fs.Length]; fs.Read(blob, 0, blob.Length); fs.Close(); param = new SqlParameter("Picture", SqlDbType.VarBinary, blob.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob); cmd.Parameters.Add(param); conn.Open(); cmd.ExecuteNonQuery(); } catch (SqlException e){ Console.Write("SQL Exception: " + e.Message()); } catch (Exception e) { Console.Write("Exception: " e.Message()); }
cmd = new SqlCommand("UPDATE Categories SET Picture =@Picture Picture WHERE new SqlParameter("Picture", byte[])
------解决方案--------------------
SqlConnection conn =null;//连接字符串来用的
SqlCommand cmd = null; //执行SQL语句
SqlParameter param = null;//传参数来用的
FileStream fs = null;//文件流
const string sConn = "server=(local);Initial
Catalog=Northwind;UID=ctester;PWD=password";//SQL的连接字符串
try {
conn = new SqlConnection(sConn);//实利化连接字符串
cmd = new SqlCommand("UPDATE Categories SET Picture = ?Picture WHERE
CategoryName = 'Seafood'", conn);//SQL语句
fs = new FileStream("c:\\Builder.doc", FileMode.Open, FileAccess.Read);//用文件流读取c:\\Builder.doc
Byte[] blob = new Byte[fs.Length];//一个Byte数组
fs.Read(blob, 0, blob.Length);//读取文件流
fs.Close();//关闭文件流
param = new SqlParameter("Picture", SqlDbType.VarBinary, blob.Length,
ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);//传入的参数
cmd.Parameters.Add(param);//添加到数据库里面
conn.Open();//关闭数据库连接
cmd.ExecuteNonQuery();//反回受影响行数
} catch (SqlException e){
Console.Write("SQL Exception: " + e.Message());//异常
} catch (Exception e) {
Console.Write("Exception: " e.Message());//异常
}
------解决方案--------------------
MySQL/MSSQL?
如果文件比较大的话,需要循环读取文件内容,。
MSSQL下面是:
UPDATETEXT { table_name.dest_column_name dest_text_ptr }
{ NULL | insert_offset }
{ NULL | delete_length }
[ WITH LOG ]
[ inserted_data
| { table_name.src_column_name src_text_ptr } ]
如果2005等较新的版本,用write 子 句的update.
USE AdventureWorks;
GO
DECLARE @MyTableVar table (
DocumentID int NOT NULL,
SummaryBefore nvarchar(max),
SummaryAfter nvarchar(max));
UPDATE Production.Document
SET DocumentSummary .WRITE (N'features',28,10)
OUTPUT INSERTED.DocumentID,
DELETED.DocumentSummary,
INSERTED.DocumentSummary
INTO @MyTableVar
WHERE DocumentID = 3 ;
SELECT DocumentID, SummaryBefore, SummaryAfter
FROM @MyTableVar;
GO