数据库存64位,读取64位转为二进制,在页面上展示

  • A+
所属分类:.NET技术
摘要

public void show()
{
   try
   {   SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings[“ConnectionString”]); //链接字符串
  connection.Open();

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

}