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

标签 c# asp.net mysql odbc

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

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

使用系统; 使用 System.Web;

公共(public)类 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;
    }
}

}

最佳答案

像这样使用连接字符串

<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

更新 使用数据网格并连接到数据库,并在数据网格中使用此代码

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

验证您的图像字段名称

关于c# - 使用 ASP/C# 在 MYSQL 中显示 blob 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10151903/

相关文章:

asp.net - 我可以获得有关 JavaScript 委托(delegate)的一些建议吗?

asp.net - 将Web API添加到现有的asp.net Web窗体应用程序

python - 由于 errno : 150 "Foreign key constraint is incorrectly formed",Django 1.8 应用程序初始迁移神秘失败

c# - 打包 .NET 应用程序以便它可以在没有 .NET 的计算机上运行

c# 什么 wpf 控件用于显示文本文件

c# - Entity Framework 错误 "Entity type is not mapped."

c# - 在C#中创建表示文件夹结构(包括子文件夹)的XML文件

c# - Asp.Net 5 语义版本控制

mysql - 选择最高的季节和剧集(mysql)

MySQL 比较运算符,空格