MySQL数据库和ASP.NET之间的连接问题 [英] Problem in the connection between MySQL database and ASP.NET

查看:64
本文介绍了MySQL数据库和ASP.NET之间的连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我做了很多次尝试在MySQL数据库和ASP.NET之间建立连接,以便创建一个Web应用程序,该应用程序允许用户将图像上传到数据库,但是不幸的是,直到现在我还是没有成功.有时,它告诉我我有问题< asp:button =" xmlns:asp =#unknown">有时它告诉我我在调用(addImage)方法时遇到问题,但我不知道.所以请你能帮我吗?

这是我将文件上传到数据库的主要代码:-


Hello everybody,

I tried many times to do a connection between MySQL database and ASP.NET in order to do a web application that let the user to upload an image to the database, but unfortunately I did not success until now. Sometimes, it told me that I have a problem <asp: button="" xmlns:asp="#unknown"> and sometimes it told me that I have a problem in the calling of (addImage) method but I don''t know. So please can you help me?

This is my primary code of uploading a file to the database:-


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HW5._Default" %>
<script>
public class DataAccess
{
    private string _strConn =
        @"Driver=   {MySQLODBC 3.51 Driver};SERVER=localhost;DATABASE=test1;";

    private OdbcConnection _objConn;

    public DataAccess()
    {
        this._objConn = new OdbcConnection(this._strConn);
    }
    // This function adds the Images to database

    public string addImage(timestamp id, byte [] data,string extension)
    {
        string strSql = "SELECT * FROM File";
        DataSet ds = new DataSet("Image");
        OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
        OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
        tempAP.Fill(ds,"Table");

        try
        {
            this._objConn.Open();
            DataRow objNewRow = ds.Tables["Table"].NewRow();
            objNewRow["Extension"] = extension;
            objNewRow["Data"] = buffer;
        objNewRow["ID"] = id;
            ds.Tables["Table"].Rows.Add(objNewRow);
            // trying to update the table to add the image
            tempAP.Update(ds,"Table");
        }
        catch(Exception e){return e.Message;}
        finally{this._objConn.Close();}
        return null;
    }0
    // This function to get the image data from the database

    public byte [] getImage(int imageNumber)
    {
        string strSql = "SELECT * FROM File";
        DataSet ds = new DataSet("Image");
        OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
        OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
        tempAP.Fill(ds,"Table");

        try
        {
            this._objConn.Open();
            byte [] buffer = (byte [])ds.Tables["Table"].Rows[imageNumber]["Data"];
            return buffer;
        }
        catch{this._objConn.Close();return null;}
        finally{this._objConn.Close();}
    }
    // Get the image count

    public int getCount()
    {
        string strSql = "SELECT COUNT(Data) FROM File";
        DataSet ds = new DataSet("Image");
        OdbcDataAdapter tempAP = new OdbcDataAdapter(strSql,this._objConn);
        OdbcCommandBuilder objCommand = new OdbcCommandBuilder(tempAP);
        tempAP.Fill(ds,"Table");

        try>
        {
            this._objConn.Open();
            int count = (int)ds.Tables["Table"].Rows[0][0];
            return count;
        }
        catch{this._objConn.Close();return 0;}
        finally{this._objConn.Close();}
    }

}

private void Page_Load(object sender, System.EventArgs e)
{
    //Checking if there are any files avaiable on IIS.
    if(Request.Files.Count != 0)
    {
        HttpPostedFile httpFile = Request.Files[0];
        // Checking for extension
        string extension = this.getFileExtension(httpFile.ContentType);
        if(extension == null )
        {
            Response.Write("Mime type not Supported");
            return;
        }
        System.IO.BufferedStream bf = new BufferedStream(httpFile.InputStream);
        byte[] buffer = new byte;
        bf.Read(buffer,0,buffer.Length);
        // Creating the database object
        DataAccess data = new DataAccess();
        // Adding files to the database.
        data.addImage(buffer,extension);
        Response.Write("Image Added!");

    }
}

</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
        <form id="form1" runat="server">


    <asp:Label

        id="lblImageFile"

        Text="Image File:"

        AssociatedControlID="upImage"

        Runat="server" />

    <asp:FileUpload

        id="upImage"

        Runat="server" />

    <br /><br />

    <asp:Button

        id="btnAdd"

        Text="Add Image"

        OnClick="DataAccess.addImage(byte [] buffer,string extension)"

        Runat="server" />

    <hr />

    <asp:DataList

        id="dlstImages"

        RepeatColumns="3"

        runat="server">
        <ItemTemplate>
        <asp:Image ID="Image1"

            ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>'

            style="width:200px"

            Runat="server" />
        <br />
        <%# Eval("Name") %>
        </ItemTemplate>
    </asp:DataList>


    </form>
</body>
</html>




问候,
matrix388




Regards,
matrix388

推荐答案

这段代码很难理解,它违反了我能想到的所有设计原则.但是,没人会阅读所有内容,然后尝试猜测您收到了什么错误消息,因为您没有告诉我们.一开始这是很难读的,但是即使是专业素质,您仍然没有告诉我们什么地方出了问题,如果您不愿意帮助我们,我们也不会努力工作.
This code is ugly as hell, it violates every design principle I can think of. However, no-one is going to read it all, then try to guess what error messages you got, b/c you do not tell us. It''s unreadable for a start, but even if it was professional quality, you still have done nothing to tell us what is wrong, and we''re not going to work hard if you won''t try to help us.


这篇关于MySQL数据库和ASP.NET之间的连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆