·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> asp.net将图片转成二进制存入数据库

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

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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

一、代码如下

 1  int code = int.Parse(this.TextBox1.Text);//图片编码 2         string value = this.FileUpload1.PostedFile.FileName.ToString();//图片路径 3         string type = value.Substring(value.LastIndexOf(".") + 1); 4         FileStream fs = File.OpenRead(value); 5         byte[] content = new byte[fs.Length]; 6         fs.Read(content, 0, content.Length); 7         fs.Close(); 8  9         string sqlconfrom = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionStrings"].ToString();10         SqlConnection con = new SqlConnection(sqlconfrom);11         string sql = " insert into Image_Table values ( '1',@content,@code ) ";12 13         SqlCommand cmd = new SqlCommand(sql, con);14         cmd.Parameters.Add(new SqlParameter("@content", SqlDbType.Image));15         cmd.Parameters["@content"].Value = content;16 17         cmd.Parameters.Add(new SqlParameter("@code", SqlDbType.Int));18         cmd.Parameters["@code"].Value = code;19         con.Open();20         int result = cmd.ExecuteNonQuery();21         if (result > 0)22         {23             Response.Write("插入成功!");24         }25         con.Close();
图片二进制存入数据库