VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • 数据库存64位,读取64位转为二进制,在页面上展示

public void show()
{
   try
   {

  SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]); //链接字符串
  connection.Open();

  StringBuilder sql = new StringBuilder();
  sql.Append("查询的sql");
  SqlCommand comm = new SqlCommand(sql.ToString(), connection);
  SqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);

  if (dr.HasRows) //判断
  {

    while (dr.Read())
    {
      Response.Clear();

      Response.BinaryWrite((byte[])dr["Image"]);//读取 

    }

  }

  else
  {//当查询数据不存在时,用暂无图片代替

    string path = System.Environment.CurrentDirectory;//非Web程序
    if (System.Environment.CurrentDirectory != AppDomain.CurrentDomain.BaseDirectory)
    {
      path = AppDomain.CurrentDomain.BaseDirectory;//asp.net 程序
      path += "Images\\without1.gif";
    }
    Response.Clear();
    using (FileStream fs = new FileStream(path, FileMode.Open)){
      byte[] byData = new byte[fs.Length];
      fs.Read(byData, 0, byData.Length);
      Response.BinaryWrite(byData);//读取  }

  }


  Response.End();
  if (connection.State == ConnectionState.Open)
  {
    connection.Close();//如果是打开状态,则关闭
  }
  }
  catch (Exception ex)
  {
  Response.Write(ex.Message);
  }

}

出处:https://www.cnblogs.com/zhai-shui/p/15076037.html


相关教程