向sql数据库中存取照片

  1. private void btnUpload_Click(object sender, System.EventArgs e) 
  2. //得到用户要上传的文件名 
  3. string strFilePathName = loFile.PostedFile.FileName; 
  4. string strFileName = Path.GetFileName(strFilePathName); 
  5. int FileLength = loFile.PostedFile.ContentLength; 
  6. if(FileLength<=0) 
  7. return
  8. try 
  9. {//上传文件 
  10. Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组 
  11. Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像 
  12. //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度 
  13. StreamObject.Read(FileByteArray,0,FileLength); 
  14. //建立SQL Server链接 
  15. string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; 
  16. SqlConnection Con = new SqlConnection(strCon); 
  17. String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)"
  18. SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); 
  19. CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray; 
  20. CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = loFile.PostedFile.ContentType; //记录文件类型 
  21. //把其它单表数据记录上传 
  22. CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = tbDescription.Text; 
  23. //记录文件长度,读取时使用 
  24. CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = FileLength; 
  25. Con.Open(); 
  26. CmdObj.ExecuteNonQuery(); 
  27. Con.Close(); 
  28. //跳转页面 
  29. Response.Redirect("ShowAll.aspx"); 
  30. catch 
  31. //2.DataGrid控件绑定数据 
  32. //程序代码 
  33. private void Page_Load(object sender, System.EventArgs e) 
  34. string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; 
  35. SqlConnection con = new SqlConnection(strCon); 
  36. SqlDataAdapter da = new SqlDataAdapter("Select * from ImageStore",con); 
  37. DataSet ds = new DataSet(); 
  38. da.Fill(ds,"image"); 
  39. dgShow.DataSource = ds.Tables["image"].DefaultView; 
  40. dgShow.DataBind(); 
  41. //3.显示数据库图片数据 
  42. 程序代码 
  43. private void Page_Load(object sender, System.EventArgs e) 
  44. int ImgID = Convert.ToInt32(Request.QueryString["ID"]); //ID为图片ID 
  45. //建立数据库链接 
  46. string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"]; 
  47. SqlConnection Con = new SqlConnection(strCon); 
  48. String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID"
  49. SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); 
  50. CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID; 
  51. Con.Open(); 
  52. SqlDataReader SqlReader = CmdObj.ExecuteReader(); 
  53. SqlReader.Read(); 
  54. Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型 
  55. //输出图象文件二进制数制 
  56. Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]); 
  57. Response.End(); 
  58. //也可以保存为图像 
  59. // FileStream fs = new FileStream(@"C:/aa.BMP", FileMode.OpenOrCreate, FileAccess.Write); 
  60. // fs.Write((byte[])SqlReader["ImageData"], 0,(int)SqlReader["ImageSize"]); 
  61. // fs.Close(); 
  62. Con.Close(); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值