asp.net将图片转成二进制存入数据库

时间:2021-06-09 07:22:44

一、代码如下

  int code = int.Parse(this.TextBox1.Text);//图片编码
string value = this.FileUpload1.PostedFile.FileName.ToString();//图片路径
string type = value.Substring(value.LastIndexOf(".") + );
FileStream fs = File.OpenRead(value);
byte[] content = new byte[fs.Length];
fs.Read(content, , content.Length);
fs.Close(); string sqlconfrom = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionStrings"].ToString();
SqlConnection con = new SqlConnection(sqlconfrom);
string sql = " insert into Image_Table values ( '1',@content,@code ) "; SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(new SqlParameter("@content", SqlDbType.Image));
cmd.Parameters["@content"].Value = content; cmd.Parameters.Add(new SqlParameter("@code", SqlDbType.Int));
cmd.Parameters["@code"].Value = code;
con.Open();
int result = cmd.ExecuteNonQuery();
if (result > )
{
Response.Write("插入成功!");
}
con.Close();

图片二进制存入数据库