求救:怎么将这个流保存到数据库中?

ggg_nj 2004-06-16 11:42:26
怎么将这个流保存到数据库中?
第三列在数据库中是ntext类型。

public void InsertFile(string reportStr,string categoryStr,System.IO.Stream stream)
{
System.Data.SqlClient.SqlCommand sqlcmd = new System.Data.SqlClient.SqlCommand(@"INSERT INTO Report (ReportCaption,CategoryCaption, XMLFile) VALUES(" + reportStr + ", " + categoryStr + "," + stream. + ")"
,conn);
try
{
sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();


}
catch(Exception ex)
{
throw ex;
}
finally
{
sqlcmd.Dispose();

}
}
...全文
55 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
netcoder 2004-06-17
  • 打赏
  • 举报
回复
HOW TO: Read and Write BLOB Data by Using ADO.NET

http://support.microsoft.com/default.aspx?scid=kb;en-us;309158
http://support.microsoft.com/default.aspx?scid=kb;EN-US;308042

private void File2SqlBlob(string SourceFilePath)
{
try
{
SqlConnection cn = new SqlConnection("server=localhost;integrated security=yes;database=NorthWind");
SqlCommand cmd = new SqlCommand("UPDATE Categories SET Picture=@Picture " +
"WHERE CategoryName='Test'", cn);

System.IO.FileStream fs =
new System.IO.FileStream(SourceFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
SqlParameter P = new SqlParameter("@Picture", SqlDbType.VarBinary, b.Length,
ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);
cmd.Parameters.Add(P);
cn.Open();
if (cmd.ExecuteNonQuery() == 1)
MessageBox.Show("Your images stored successfully");
cn.Close();
}
catch(SqlException ex)
{
MessageBox.Show (ex.Message);
}
}

public void File2OleDbBlob(string SourceFilePath)
{
try
{
OleDbConnection cn = new OleDbConnection("provider=sqloledb;server=localhost;" +
"user id=uid;password=password;initial catalog=NorthWind");

OleDbCommand cmd = new OleDbCommand("UPDATE Categories SET Picture=? WHERE CategoryName='Test'", cn);
System.IO.FileStream fs =
new System.IO.FileStream(SourceFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
OleDbParameter P = new OleDbParameter("@Picture", OleDbType.VarBinary, b.Length,
ParameterDirection.Input, false, 0, 0, null,DataRowVersion.Current, b);

cmd.Parameters.Add(P);
cn.Open();
if (cmd.ExecuteNonQuery() == 1)
MessageBox.Show("Your images stored successfully");
cn.Close();
}
catch(OleDbException ex)
{
MessageBox.Show (ex.Message);
}
}

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧