首页 »DotNet » 数据库连接字符串:利用数据库存储文本文件、图像文件需要的字符串读写思路方法备忘 »正文
数据库连接字符串:利用数据库存储文本文件、图像文件需要的字符串读写思路方法备忘
来源: 发布时间:星期三, 2009年1月14日 浏览:54次 评论:0
读写大文本为防止注入等各种问题 ![](/icons/79049dou.gif) 将文本转换为Unicode或UTF8进行保存 /// <summary> /// 将文本 串转换成带","号分离 2进制 串 /// </summary> /// <param name="strContent">文本 串</param> /// < s>带,号分离 2进制 串</ s> private strTextTostrBin( strText) { ![](/icons/79049byte.gif) arr=null; tobin=""; .Text.Encoding encoding = .Text.Encoding.UTF8; arr=encoding.GetBytes(strText); for( i=0;i< arr.Length;i ) { tobin ","+ arr[i].![](/icons/79049ToString.gif) ; } tobin.Sub (1); } /// <summary> /// 将带,号分离 2进制 串转换成文本 串 /// </summary> /// <param name="strBin">带,号分离 2进制 串</param> /// < s>文本 串</ s> private strBinTostrText( strBin) { b ostr=strBin.Split(','); Array binArray=Array.CreateInstance(Type.GetType(" .Byte"),b ostr.Length); for( i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i ) { binArray.SetValue( .Parse(b ostr[i]+""),i); } ![](/icons/79049byte.gif) strtobin= [b ostr.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#如何存取图像 图像文件 ![](/icons/79049de.gif) 写入 ![](/icons/79049dou.gif) 使用文件上传Control控件将数据读入流中 ![](/icons/79049dou.gif) 再写入 2字节 ![](/icons/79049shuzu.gif) 中 ![](/icons/79049dou.gif) 直接写入数据库 ![](/icons/79049dou2.gif) private void ImgDataReadWrite![](/icons/79049kh.gif) { HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile对象 用于读取图象文件属性 FileLength = UpFile.ContentLength; try { (FileLength 0) { lblMessage.Text = "<b>请选择您要上传 文件</b>"; } ![](/icons/79049else.gif) { Byte FileByteArray = [FileLength]; //图象文件临时储存Byte![](/icons/79049shuzu.gif) 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.![](/icons/79049ToString.gif) ; } } 图像文件 ![](/icons/79049de.gif) 读取 ![](/icons/79049dou.gif) 直接写入流 private void ImgDataRead![](/icons/79049kh.gif) { 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((![](/icons/79049byte.gif) )SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"])); Response.BufferOutput = true; //或 ![](/icons/79049byte.gif) s= (![](/icons/79049byte.gif) )SqlReader["ImageData"]; // MemoryStream memStream= MemoryStream( s); // try // { // Bitmap myImage = Bitmap(memStream); // this.pictureBox1.Image= myImage; // } // catch // { // this.pictureBox1.Image=null; // } Con.Close ; }
相关文章
读者评论
发表评论
|
|