本文实例讲述了C#操作数据库中存取图片文件的方法。分享给大家供大家参考。具体如下: private string sqlconnstr = "Data Source=.;Database=db_test;User id=sa;PWD=123456";
/*功能:把一种图片插入到数据库中
*返回值:无
*/
void InsertImageToDB()
{
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(@"D:/Bear.jpg", FileMode.Open, FileAccess.Read);
Byte[] byte_fs = new byte[fs.Length];
fs.Read(byte_fs, 0, Convert.ToInt32(fs.Length));
fs.Close();
//建立数据库连接
SqlConnection conn = new SqlConnection(sqlconnstr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into tb_test(image_id,image_file) values(@image_id,@image_file)";
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@imag