使用ASP / C#在MYSQL中显示blob图像

时间:2012-04-14 07:34:16

标签: c# asp.net mysql odbc

我需要使用带有C#的ASP页面中的ODBC连接,将存储在mysql db中的图像显示为blob字段。请帮帮我

<%@ WebHandler Language="C#" Class="stdImg" %>

使用System; 使用System.Web;

public class stdImg:IHttpHandler {

public void ProcessRequest(HttpContext context)
{


    System.Data.Odbc.OdbcConnection con = new System.Data.Odbc.OdbcConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sis"].ConnectionString;


    con.Open();
    System.Data.Odbc.OdbcCommand cmd = con.CreateCommand();
    cmd.CommandText = "SELECT PHOTOGRAPH FROM student_mast WHERE ADMISSION_NO='1000000001'";


    byte[] buf = (byte[])cmd.ExecuteScalar();


    context.Response.Clear();
    context.Response.OutputStream.Write(buf, 0, buf.Length);
    context.Response.ContentType = "image/jpeg";
    context.Response.BinaryWrite(buf);
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

1 个答案:

答案 0 :(得分:1)

使用像这样的连接字符串

<add name="MYSQLConnectionString" connectionString="Driver={MySQL ODBC 3.51 Driver};database=DB;option=0;pwd=pwd;port=3306;server=yourserver;uid=user;sslverify=0"
   providerName="System.Data.Odbc" />

首先安装MYSQl的ODBC驱动程序

这里是下载链接

http://dev.mysql.com/downloads/connector/odbc/3.51.html

<强>更新 使用数据网格并建立与DB的连接,并在数据网格中使用此代码

 <asp:TemplateColumn  HeaderText="Image">
         <ItemTemplate >
            <img runat="server" 
                 src='<%# "getImage.aspx?ID=" + DataBinder.Eval(Container.DataItem, 
                 "ImageIdentity")  %>' ID="Img1"/>
         </ItemTemplate>
     </asp:TemplateColumn>

验证您的图片字段名称