您的位置:首页 > 数据库

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

2009-01-14 10:18 676 查看
读写大文本为防止注入等各种问题,将文本转换为Unicode或UTF8进行保存

/// <summary>
/// 将文本字符串转换成带","号分离的二进制字符串
/// </summary>
/// <param name="strContent">文本字符串</param>
/// <returns>带,号分离的二进制字符串</returns>
private string strTextTostrBin(string strText)
{
byte[] bytearr=null;

string stringtobin="";
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
bytearr=encoding.GetBytes(strText);

for(int i=0;i<bytearr.Length;i++)
{
stringtobin+=","+bytearr[i].ToString();
}
return stringtobin.Substring(1);

}

/// <summary>
/// 将带,号分离的二进制字符串转换成文本字符串
/// </summary>
/// <param name="strBin">带,号分离的二进制字符串</param>
/// <returns>文本字符串</returns>
private string strBinTostrText(string strBin)
{
string [] bintostr=strBin.Split(',');
Array binArray=Array.CreateInstance(Type.GetType("System.Byte"),bintostr.Length);
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
binArray.SetValue(byte.Parse(bintostr[i]+""),i);
}

byte[] strtobin=new byte[bintostr.Length];
for(int i=binArray.GetLowerBound(0);i<=binArray.GetUpperBound(0);i++)
{
strtobin[i]=(byte)binArray.GetValue(i);
}
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(strtobin);
}

C#如何存取图像

图像文件的写入,使用文件上传控件将数据读入流中,再写入二字节数组中,直接写入数据库。
private void ImgDataReadWrite()
{
HttpPostedFile UpFile = UP_File.PostedFile;//HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength;

try
{
if (FileLength == 0)
{
lblMessage.Text = "<b>请选择您要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObj = UpFile.InputStream;//建立数据流对像

//或Stream myStream = openFileDialog1.OpenFile();
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObj.Read(FileByteArray, 0, FileLength);

SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
String SqlCmd = "INSERT INTO ImageStore (ImageData,ImageContentType,ImageDescription,ImageSize) VALUES (@Image,@ContentType,@ImageDescription,@ImageSize)";
SqlCommand CmdObj = new 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.ToString();
}

}
图像文件的读取,直接写入流

private void ImgDataRead()
{
int ImgID = Convert.ToInt32(Request.QueryString["id"]);

SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
//输出图象文件二进制数制

Response.OutputStream.Write((byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader["ImageSize"]));

Response.BufferOutput = true;

//或 byte[] bytes= (byte[])SqlReader["ImageData"];
//   MemoryStream memStream=new MemoryStream(bytes);
//   try
//   {
//   Bitmap myImage = new Bitmap(memStream);
//   this.pictureBox1.Image= myImage;
//   }
//   catch
//   {
//   this.pictureBox1.Image=null;
//   }

Con.Close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐