利用数据库存储文本文件、图像文件需要的字符串读写方法备忘
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();
}
/// <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();
}
相关文章推荐
- springbatch 读取csv文件时 文件中有字符串需要转换为Date类型的字段报错字符串不能转化为Date类型的解决方法
- Android手机数据读写方法(内部存储、SD卡,网络加载,包内文件读取)
- Lucene4.2源码解析之fdt和fdx文件的读写(续)——fdx文件存储一个个的Block,每个Block管理着一批Chunk,通过docID读取到document需要完成Segment、Block、Chunk、document四级查询,引入了LZ4算法对fdt的chunk docs进行了实时压缩/解压
- 利用数据库文件恢复Oracle数据库的简单方法
- asp.net当文件在不同目录下,需要获取数据库连接字符串收藏
- 利用C#进行文件读写的方法选择总结
- 注册表中存储数据库链接字符串的方法
- 字符串常用方法—文件内容读写
- Android+SQLite将txt等文本文件转化为db数据库文件的方法
- Android+SQLite将txt等文本文件转化为db数据库文件的方法
- 你想不到的压缩方法:将javascript文件压缩成PNG图像存储
- 利用存储过程数据库中的数据生成txt文件
- 使用Hibernate连接h2数据库的配置文件写的方法(连接字符串)
- 利用C#进行文件读写的方法选择总结
- 掌握文本文件和二进制文件读写的方法和二进制文件
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- c#里把数据库链接字符串写在web.config文件里调用方法
- 如何利用C/C++逐行读取txt文件中的字符串(可以顺便实现文本文件的复制)
- asp.net当文件在不同目录下,需要获取数据库连接字符串
- C++中利用文件输入对象(即ifstream对象)来读取文本文件中带空格的字符串以及混合输入数字与字符串