ASP.NET 使用 SqlConnection 连接 MySQL [英] ASP.NET use SqlConnection connect MySQL

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

问题描述

这是web.config中保存的连接字符串:

This is the connection string saved in 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>

这是连接数据库的代码:

This is the code to connect to the database:

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失败,返回错误:

But it fails to connect MySQL, with this return error:

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 数据库?

Is that possible SqlConnection to connect MySQL database?

推荐答案

SqlConnection 适用于 SQL Server.您需要 MySqlConnection - 这不是 .NET Framework 的一部分,因此您必须 下载它 并在您的项目中引用它.然后,您可以创建一个 MySqlConnection 对象并在您的应用程序中连接到 MySQL:

SqlConnection is for SQL Server. You need MySqlConnection - this is not part of the .NET Framework, so you will have to download it and reference it in your project. You can then create a MySqlConnection object and connect to MySQL in your application:

MySqlConnection connection = new MySqlConnection(myConnString);

您还必须使用 MySqlCommand 对象而不是 SqlCommand 对象.

You will also have to use the MySqlCommand object rather than the SqlCommand object.

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

这篇关于ASP.NET 使用 SqlConnection 连接 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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