在asp.net中显示数据库中不同列的两张照片

时间:2014-07-07 03:04:10

标签: c# asp.net sql-server

这是我现在使用Asp.net和C#

的代码
string img1 = Request.QueryString["cn"];

if (img1 != null)
{
   //string str = "select mem_contenttype, mem_photo from tblCardRequestDetail2 where mem_cardno = '" + Request.QueryString["cn"] + "'";
   string str = "select mem_contenttype1, mem_photo1 from tblphotoupload where mem_cardno = '" + img1 + "'";

   SqlCommand cmd = new SqlCommand(str); cmd.Parameters.Add("@cn", SqlDbType.VarChar).Value = img1;
   //cmd.Parameters.AddWithValue("@cn", Request.QueryString["cn"].ToString());

   DataTable dt = GetData(cmd);

   if (img1 != null)
   {
      Byte[] bytes = (Byte[])dt.Rows[0]["mem_photo1"];
      Response.OutputStream.Write(bytes, 0, bytes.Length);
      Response.Buffer = true;
      Response.Charset = "";
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.ContentType = dt.Rows[0]["mem_contenttype1"].ToString();
      Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["mem_photo1"].ToString());
      Response.BinaryWrite(bytes);
      Response.Flush();
      Response.End();
   }
}

string img2 = Request.QueryString["cn"];

if (img2 != null)
{
   string str2 = "select mem_contenttype2, mem_photo2 from tblphotoupload where mem_cardno = '" + img2 + "'";
   SqlCommand cmd2 = new SqlCommand(str2); cmd2.Parameters.Add("@cn2", SqlDbType.VarChar).Value = img2;
   //cmd.Parameters.AddWithValue("@cn", Request.QueryString["cn"].ToString());

   DataTable dt2 = GetData2(cmd2);

   Byte[] bytes = (Byte[])dt2.Rows[0]["mem_photo2"];
   Response.OutputStream.Write(bytes, 0, bytes.Length);
   Response.Buffer = true;
   Response.Charset = "";
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.ContentType = dt2.Rows[0]["mem_contenttype2"].ToString();
   Response.AddHeader("content-disposition", "attachment;filename=" + dt2.Rows[0]["mem_photo2"].ToString());
   Response.BinaryWrite(bytes);
   Response.Flush();
   Response.End();
}



}
private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["EKConn"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
    catch
    {
        return null;
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
    }
}
private DataTable GetData2(SqlCommand cmd2)
{
    DataTable dt2 = new DataTable();
    String strConnString2 = System.Configuration.ConfigurationManager.ConnectionStrings["EKConn"].ConnectionString;
    SqlConnection con2 = new SqlConnection(strConnString2);
    SqlDataAdapter sda2 = new SqlDataAdapter();
    cmd2.CommandType = CommandType.Text;
    cmd2.Connection = con2;
    try
    {
        con2.Open();
        sda2.SelectCommand = cmd2;
        sda2.Fill(dt2);
        return dt2;
    }
    catch
    {
        return null;
    }
    finally
    {
        con2.Close();
        sda2.Dispose();
        con2.Dispose();
    }
}

它只显示第一张照片(mem_photo1)我无法显示第二张照片..

我想看到的是mem_photo1mem_photo2的图片。这可能吗 ?

0 个答案:

没有答案