日期:2014-05-19  浏览次数:20863 次

没分了,各位帮忙 如何将文件或图片写入数据库,并读取出来 ●●●●
如题,谢谢各位了,给点代码!~!

------解决方案--------------------
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
------解决方案--------------------
存储(代码可能有点问题,是从几个文件里拿出来的,节约篇幅)

string strSql = "insert into img values(@img) ";

FileStream fs = new FileStream(this.txtFileName.text,FileMode.Open,FileAccess.Read);

byte[] contents = new byte[fs.Length];

try
{
fs.Read(contents,0,contents.Length);
}
catch(Exception ex)
{
throw ex;
}
finally
{
fs.Close();
}

SqlParameter[] parms = new SqlParameter[1];
parms[0] = new SqlParameter( "@img ",SqlDbType.Image);
parms[0].Value = contents;

string ConnectinStr = "server=.;database=pubs;uid=sa;pwd= ";

SqlConnection conn = new SqlConnection(ConnectinStr);
SqlCommand cmd = new SqlCommand(strSql,conn);

if(parms != null)
{
foreach(SqlParameter parm in parms)
{
cmd.Parameters.Add(parm);
}
}

try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}