c#中byte[]和string的转换
2013-01-28 16:03
176 查看
Using System.Text;
byte[ ] 转换为string
byte[ ] image;
string ll = Encoding.Default.GetString(image);
string 转换为byte[ ]
string ss;
byte[] b = Encoding.Default.GetBytes(ss);
数据库中image类型的字段的处理。
首先我想从数据库中读出图片(以image类型存储的), 并且写入txt文件中:
private void GetImage()
{
string conn = "Server=192.168.0.11; User id=user; Pwd=pwd; Database=database";
SqlConnection sqlCon = new SqlConnection(conn);
string sql = "SELECT ImageFile, PersonID FROM Persons where PersonID = 1";
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = sqlCon;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = sql;
DataTable myTable = new DataTable();
SqlDataAdapter myDataAda = new SqlDataAdapter();
myDataAda.SelectCommand = myCommand;
try
{
sqlCon.Open();
myDataAda.Fill(myTable);
sqlCon.Close();
if(myTable.Rows.Count>0)
{
byte[] image = (byte[])myTable.Rows[0]["ImageFile"];
string ll = Encoding.Default.GetString(image);
WriteStr(@"F:\test.txt",ll);
}
}
catch(Exception ex)
{
string err = ex.Message;
}
finally
{
sqlCon.Close();
myCommand.Dispose();
myDataAda.Dispose();
}
}
private void WriteStr(string strLogFileName, string strLogContent)
{
try
{
FileInfo objFileInfo = new FileInfo(strLogFileName);
using (FileStream objFileStream = objFileInfo.OpenWrite())
{
using (StreamWriter objStreamWriter = new StreamWriter(objFileStream))
{
objStreamWriter.BaseStream.Seek(0, SeekOrigin.End);
objStreamWriter.Write("{0}", strLogContent);
}
}
}
catch
{
}
}
其次,需要把图片从txt文件中读出然后存入数据库中。
string ss = ReadStr(@"F:\test.txt");
byte[] b = Encoding.Default.GetBytes(ss);
string conn = "Server=server; User id=user; Pwd=pwd; Database=database";
SqlConnection sqlCon = new SqlConnection(conn);
string sql = "update Persons set ImageFile=@img where PersonID = 1";
SqlCommand myCommand = new SqlCommand();
SqlParameter sp = new SqlParameter("@img",SqlDbType.Image);
myCommand.Connection = sqlCon;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = sql;
sp.Value = b;
myCommand.Parameters.Add(sp);
try
{
sqlCon.Open();
myCommand.ExecuteNonQuery();
sqlCon.Close();
}
catch(Exception eS)
{
string ee = eS.Message;
}
finally
{
sqlCon.Close();
myCommand.Dispose();
}
byte[ ] 转换为string
byte[ ] image;
string ll = Encoding.Default.GetString(image);
string 转换为byte[ ]
string ss;
byte[] b = Encoding.Default.GetBytes(ss);
数据库中image类型的字段的处理。
首先我想从数据库中读出图片(以image类型存储的), 并且写入txt文件中:
private void GetImage()
{
string conn = "Server=192.168.0.11; User id=user; Pwd=pwd; Database=database";
SqlConnection sqlCon = new SqlConnection(conn);
string sql = "SELECT ImageFile, PersonID FROM Persons where PersonID = 1";
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = sqlCon;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = sql;
DataTable myTable = new DataTable();
SqlDataAdapter myDataAda = new SqlDataAdapter();
myDataAda.SelectCommand = myCommand;
try
{
sqlCon.Open();
myDataAda.Fill(myTable);
sqlCon.Close();
if(myTable.Rows.Count>0)
{
byte[] image = (byte[])myTable.Rows[0]["ImageFile"];
string ll = Encoding.Default.GetString(image);
WriteStr(@"F:\test.txt",ll);
}
}
catch(Exception ex)
{
string err = ex.Message;
}
finally
{
sqlCon.Close();
myCommand.Dispose();
myDataAda.Dispose();
}
}
private void WriteStr(string strLogFileName, string strLogContent)
{
try
{
FileInfo objFileInfo = new FileInfo(strLogFileName);
using (FileStream objFileStream = objFileInfo.OpenWrite())
{
using (StreamWriter objStreamWriter = new StreamWriter(objFileStream))
{
objStreamWriter.BaseStream.Seek(0, SeekOrigin.End);
objStreamWriter.Write("{0}", strLogContent);
}
}
}
catch
{
}
}
其次,需要把图片从txt文件中读出然后存入数据库中。
string ss = ReadStr(@"F:\test.txt");
byte[] b = Encoding.Default.GetBytes(ss);
string conn = "Server=server; User id=user; Pwd=pwd; Database=database";
SqlConnection sqlCon = new SqlConnection(conn);
string sql = "update Persons set ImageFile=@img where PersonID = 1";
SqlCommand myCommand = new SqlCommand();
SqlParameter sp = new SqlParameter("@img",SqlDbType.Image);
myCommand.Connection = sqlCon;
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = sql;
sp.Value = b;
myCommand.Parameters.Add(sp);
try
{
sqlCon.Open();
myCommand.ExecuteNonQuery();
sqlCon.Close();
}
catch(Exception eS)
{
string ee = eS.Message;
}
finally
{
sqlCon.Close();
myCommand.Dispose();
}
相关文章推荐
- string和byte[]的转换 (C#)
- string和byte[]的转换 (C#)
- [收藏]C#中string与byte[]的转换帮助类-.NET教程,C#语言
- C#中byte[]与string的转换
- c#中 uint--byte[]--char[]--string相互转换
- C#中string和byte[]的转换
- c#中byte[]和string的转换
- c#中byte[]和string的转换
- C#中String转换为sbyte*的方法
- C# string类型和byte[]类型相互转换
- C#中string转换成字节byte的处理方法
- c#中的char byte string 类型之间的转换
- C# 图片与byte[]之间以及byte[]与string之间的转换
- C#中byte[]与string的转换
- c#中 uint--byte[]--char[]--string相互转换汇总
- string和byte[]的转换 (C#)
- c#中byte[]和string的转换
- C# byte[]类型和String类型相互转换
- winform C#中Byte与String的转换方法,相互转换
- C# char[]、byte[]、string互相转换