數據庫存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);
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM