专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » 数据库连接字符串:利用数据库存储文本文件、图像文件需要的字符串读写思路方法备忘 »正文

数据库连接字符串:利用数据库存储文本文件、图像文件需要的字符串读写思路方法备忘

来源: 发布时间:星期三, 2009年1月14日 浏览:54次 评论:0
  读写大文本为防止注入等各种问题将文本转换为Unicode或UTF8进行保存

/// <summary>
 /// 将文本串转换成带","号分离 2进制
 /// </summary>
 /// <param name="strContent">文本串</param>
 /// <s>带,号分离 2进制串</s>
 private strTextTostrBin( strText)
 {
  arr=null;
  tobin="";
 .Text.Encoding encoding = .Text.Encoding.UTF8;
 arr=encoding.GetBytes(strText);
 for( i=0;i<arr.Length;i)
 {
 tobin","+arr[i].;
 }
  tobin.Sub(1);
 
 }
 /// <summary>
 /// 将带,号分离 2进制串转换成文本
 /// </summary>
 /// <param name="strBin">带,号分离 2进制串</param>
 /// <s>文本串</s>
 private strBinTostrText( strBin)
 {
  bostr=strBin.Split(',');
 Array binArray=Array.CreateInstance(Type.GetType(".Byte"),bostr.Length);
 for( i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i)
 {
 binArray.SetValue(.Parse(bostr[i]+""),i);
 }
 
  strtobin= [bostr.Length];
 for( i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i)
 {
 strtobin[i]=()binArray.GetValue(i);
 }
 .Text.Encoding encoding = .Text.Encoding.UTF8;
  encoding.GetString(strtobin);
 }


  C#如何存取图像

  图像文件写入使用文件上传Control控件将数据读入流中再写入 2字节直接写入数据库

private void ImgDataReadWrite
 {
 HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile对象用于读取图象文件属性
 FileLength = UpFile.ContentLength;
 try
 {
  (FileLength 0)
 {
 lblMessage.Text = "<b>请选择您要上传文件</b>";
 }
 
 {
 Byte FileByteArray = [FileLength]; //图象文件临时储存Byte
 Stream StreamObj = UpFile.InputStream;//建立数据流对像
  
//或Stream myStream = openFileDialog1.OpenFile;
 //读取图象文件数据FileByteArray为数据储存体0为数据指针位置、FileLnegth为数据长度
 StreamObj.Read(FileByteArray, 0, FileLength);
 SqlConnection Con = SqlConnection(.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
 String SqlCmd = "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
 SqlCommand CmdObj = SqlCommand(SqlCmd, Con);
 CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
 CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType;//记录文件类型
 //把其它单表数据记录上传
 CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
 //记录文件长度读取时使用
 CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
 Con.Open;
 CmdObj.ExecuteNonQuery;
 Con.Close;
 lblMessage.Text = "<p><b>OK!你已经成功上传你图片</b>";//提示上传成功
 }
 }
 catch (Exception ex)
 {
 lblMessage.Text = ex.Message.;
 }
 }




  图像文件读取直接写入流

private void ImgDataRead
 {
  ImgID = Convert.ToInt32(Request.QueryString["id"]);
 SqlConnection Con = SqlConnection(.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
 String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
 SqlCommand CmdObj = SqlCommand(SqlCmd, Con);
 CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
 Con.Open;
 SqlDataReader SqlReader = CmdObj.ExecuteReader;
 SqlReader.Read;
 Response.ContentType = ()SqlReader["ImageContentType"];//设定输出文件类型
 //输出图象文件 2进制数制
 
 Response.OutputStream.Write(()SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));
  
Response.BufferOutput = true;
  
//或 s= ()SqlReader["ImageData"];
//   MemoryStream memStream= MemoryStream(s);
//   try
//   {
//   Bitmap myImage = Bitmap(memStream);
//   this.pictureBox1.Image= myImage;
//   }
//   catch
//   {
//   this.pictureBox1.Image=null;
//   }
 Con.Close;
 }




1

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: