ASP.NET使用SqlConnection连接MySQL

9

以下是保存在 web.config 中的连接字符串:

<appSettings>
    <add key="conn" value="Driver={MySQL ODBC 5.1 Driver};server=127.0.0.1;uid=root;pwd=1234;database=gis_server;option=3"/>
    </appSettings>

这是连接数据库的代码:
protected bool CheckPasswordBySqlServer(string strEmail, string strPsw)
{
    if (strEmail.ToLower() == "admin")
    {
        return false;
    }
    string str = "select id,Rank,RankEnc,ParentUser,Company from tbl_User where userName=@UserName and password1=@password";
    private string strConn = ConfigurationManager.AppSettings["conn"].ToString();
    SqlConnection sqlConnection = new SqlConnection(strConn);
    bool flag = false;
    try
    {
        try
        {
            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand(str, sqlConnection);
            sqlCommand.Parameters.AddWithValue("UserName", strEmail);
            sqlCommand.Parameters.AddWithValue("Password", strPsw);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (!sqlDataReader.Read())
            {
                flag = false;
            }
            else
            {
                this.Session["UserName"] = strEmail;
                this.Session["Password"] = strPsw;
                this.Session["LoginType"] = "Group";
                this.Session["FullName"] = sqlDataReader["Company"].ToString();
                if (FormsAuthentication.HashPasswordForStoringInConfigFile(string.Concat(strEmail, (char)43, sqlDataReader["Rank"].ToString()).ToLower(), "MD5") != sqlDataReader["RankEnc"].ToString().Trim())
                {
                    flag = false;
                }
                this.Session["ClientID"] = sqlDataReader["id"].ToString();
                this.Session["MyLanguage"] = base.Request.Cookies["Language"].Value;
                this.Session["ParentUser"] = sqlDataReader["ParentUser"].ToString().Trim();
                this.Session["Rank"] = sqlDataReader["Rank"].ToString();
                this.Session["strConnection"] = this.strConn;
                flag = true;
            }
            sqlDataReader.Close();
        }
        catch (Exception exception)
        {
            this.SetlblInfoHtml(exception.Message);
        }
    }
    finally
    {
        sqlConnection.Close();
    }
    return flag;
}

但它无法连接MySQL,返回以下错误:
System.ArgumentException: Keyword not supported: 'driver'. at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at System.Data.SqlClient.SqlConnection..ctor(String connectionString) at Source_LoginFrm.CheckPasswordBySqlServer(String strEmail, String strPsw) at Source_LoginFrm.btnLogin_Click(String strLang)

能否使用SqlConnection连接MySQL数据库?


你看过http://www.connectionstrings.com/mysql/吗? - Jonesopolis
遇到了同样的问题,以下是帮助我的两个链接:为了检查MySQL配置是否正确,我按照页面上的说明进行操作: MySQL EF6 Support 我在同一个项目中使用了MS SQL和MySQL,然后不得不添加一个DBConfiguration,如下所述: DBConfiguration for MS SQL and MySQL - Ricardo stands with Ukraine
3个回答

28

SqlConnection 用于 SQL Server。你需要使用 MySqlConnection - 这不是 .NET Framework 的一部分,所以你需要下载它并在你的项目中引用它。之后你就可以创建一个 MySqlConnection 对象并在应用程序中连接到 MySQL:

MySqlConnection connection = new MySqlConnection(myConnString);

您还需要使用 MySqlCommand 对象,而不是 SqlCommand 对象。

http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html


JDBC更好。 - Alex78191

4

如Darren所说,“SqlConnection是为SQL Server准备的”。您需要从NuGet安装MySql.Data:mySql.Data

您也可以在包管理器控制台中使用Install-Package MySql.Data命令。

然后,您就可以创建MySqlConnection对象并连接到您的数据库:

var cnn = new MySqlConnection("my Connection String");

0
我不知道有没有这样的方法,即使有也是为什么要这样做呢?你正在使用专门为Microsoft SQL Server创建的连接对象,因此它不会像MySQL那样进行连接。
如果要访问MySQL数据库,应该使用MySQL .NET连接器,您可以在这里找到here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接