简体   繁体   English

MySQL的连接字符串(asp.net中的C#)

[英]Connection String for MySQL (C# in asp.net)

I am using a the C# ODBC object in asp.net to connect up to a MySQL server hosted on another computer (on the same network). 我在asp.net中使用C#ODBC对象连接到托管在另一台计算机(同一网络)上的MySQL服务器。

<%@ Import Namespace="System.Data.Odbc" %>
<html>
<body>
<script language="C#" runat="server">

protected void Page_Load(Object Src, EventArgs E) 
{
try
{
using(OdbcConnection connection = new OdbcConnection("DRIVER={MySQL ODBC 5.51.30 Driver};Database=test;Server=192.168.1.109;UID=Username;PWD=Password;"))
{
    connection.Open();
    using(OdbcCommand command = new OdbcCommand("SELECT * FROM tablename", connection))
    using(OdbcDataReader dr = command.ExecuteReader())
    {
        while(dr.Read())
            Response.Write(dr["name"].ToString() + "<br>");
        dr.Close();
    }
    connection.Close();
}
}
catch(Exception ex)
{
Response.Write("An error occurred: " + ex.Message);
}
}
</script>
</body>
</html>

I am currently getting this error when I run the code: 我现在在运行代码时收到此错误:

An error occurred: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I am hosting this on a Microsoft IIS server, with ASP.net enabled. 我将其托管在启用了ASP.net的Microsoft IIS服务器上。 Would I be able to get any help with fixing this error? 我可以在解决此错误方面获得任何帮助吗?

Link to image of my MySQL server: http://i.stack.imgur.com/4NYCr.png 链接到我的MySQL服务器的图像: http : //i.stack.imgur.com/4NYCr.png

Would I be able to get any help with fixing this error? 我可以在解决此错误方面获得任何帮助吗?

Yes, you would. 是的,你会的。 So, your only question was answered. 因此,您唯一的问题得到了回答。 And now? 现在? Please think a little more about WHAT you ask. 请多考虑您的要求。

TO your problem. 给你的问题。

Like always, http://www.connectionstrings.com/ is the primary resource for this ;) Anything you ever wanted to know about connectionstrings on one place. 像往常一样, http://www.connectionstrings.com/是此内容的主要资源;)您想知道的有关某个地方的连接字符串的任何信息。

For MySQL (asuming 5.2) you can find (for ODBC) the solution at: 对于MySQL(低于5.2),您可以在以下位置找到(对于ODBC)解决方案:

http://www.connectionstrings.com/mysql-connector-odbc-5-2/ http://www.connectionstrings.com/mysql-connector-odbc-5-2/

Now, your string looks good - so I start assuming you have a driver problem. 现在,您的字符串看起来不错-所以我开始假设您遇到驱动程序问题。 I would assume that wherever you run it it just does not have - the ODBC ddriver installed. 我认为无论您在哪里运行它都没有-安装了ODBC ddriver。

THAT SAID: there is no real reason to use ODBC (which has native prerequisites) and not a (managed) MySQL Driver? 说:没有真正的理由使用ODBC(具有本地先决条件)而不使用(托管的)MySQL驱动程序吗?

if you really miss the ODBC driver, you can ind the downloads at http://www.mysql.com/products/connector/ . 如果您确实错过了ODBC驱动程序,则可以在http://www.mysql.com/products/connector/上找到下载内容。 But I would really not use ODBC here - not with a native connector available. 但是我真的不会在这里使用ODBC-没有可用的本机连接器。

I think you better use MySQlConnection as well as MyqlCommand for your needs: eg: 我认为您最好根据需要使用MySQlConnectionMyqlCommand :例如:

 server = "localhost";
        database = "connectcsharptomysql";
        uid = "username";
        password = "password";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM