Cc uploading pictures to the database

From , 5 Years ago, written in C#, viewed 154 times.
URL https://pastebin.vip/view/e546594a
  1. protected void Button2_Click(object sender, EventArgs e)
  2.   {
  3.   string lastName = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
  4.  
  5.   if (lastName.ToLower() == "jpg" || lastName.ToLower() == "bmp" || lastName.ToLower() == "gif")
  6.   {
  7.   int len = FileUpload1.PostedFile.ContentLength;
  8.   Byte[] pictureData = new byte[len]; //将图片转换成二进制
  9.   HttpPostedFile htp = FileUpload1.PostedFile;
  10.   Stream srm = htp.InputStream;
  11.   srm.Read(pictureData, 0, len);
  12.   SqlConnection conn = new SqlConnection("Data Source=tiger;Initial Catalog=liuyan;Integrated Security=True");
  13.   conn.Open();
  14.   string sqlsel = "select * from tb_pic";
  15.   SqlDataAdapter ada = new SqlDataAdapter(sqlsel, conn);
  16.   DataSet ds = new DataSet();
  17.   ada.Fill(ds);
  18.   DataList1.DataSource = ds;
  19.   DataList1.DataBind();
  20.   SqlCommand comd = new SqlCommand("insert into tb_pic (username,name,pic) values ('" + TextBox4.Text + "','" + FileUpload1.FileName + "',@pictureData)", conn);
  21.   comd.Parameters.Add("@pictureData", SqlDbType.Image);
  22.   comd.Parameters["@pictureData"].Value = pictureData;
  23.   comd.ExecuteNonQuery();
  24.   DataList1.DataBind();
  25.   conn.Close();
  26.  
  27.   Response.Write("<script>alert('添加成功!')location='tupian.aspx'</script>");
  28.   }
  29.   }
  30. //csharp/821

Reply to "Cc uploading pictures to the database"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website