博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bytes 与 string 互转,stream to Byte[]
阅读量:5174 次
发布时间:2019-06-13

本文共 2686 字,大约阅读时间需要 8 分钟。

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();
    }

 

Stream To Byte[]

 public static byte[] StreamToByte(Stream Reader)

        {
            try
            {
                MemoryStream mem = new MemoryStream(1024 * 500);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                int TotalByteRead = 0;
                while (true)
                {
                    bytesRead = Reader.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                        break;
                    TotalByteRead += bytesRead;
                    mem.Write(buffer, 0, buffer.Length);
                }
                if (mem.Length > 0)
                {
                    return mem.ToArray();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ep)
            {
                throw ep;
            }
        }

转载于:https://www.cnblogs.com/jianshao810/archive/2010/09/30/1839571.html

你可能感兴趣的文章
CFileDialog的使用方法简单介绍
查看>>
send,recv,sendto,recvfrom
查看>>
C#开发问题汇总
查看>>
Kettle
查看>>
[复习]Python基础回顾
查看>>
LNMP
查看>>
java 读写锁
查看>>
_itoa_s替换 itoa
查看>>
Nginx负载均衡
查看>>
【bzoj3456】城市规划(多项式求逆+dp)
查看>>
#ifdef 支持Mac #ifndef 支持Windows #if defined (Q_OS_WIN) 应该可以再两个系统通用
查看>>
linux源码中的核心数据结构
查看>>
EF执行SQL语句
查看>>
Ogre学习教程:Ogre1.8.1+VS2010环境配置2(转)
查看>>
webpack 样式表抽离成专门的单独文件并且设置版本号
查看>>
个人作业week7——前端开发感想总结
查看>>
VC Dimension -衡量模型与样本的复杂度
查看>>
android 中 ViewPager 的平常用法 ViewPager+ Views
查看>>
POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
查看>>
ZOJ 1654 二分匹配基础题
查看>>