当前位置: 首页 > 面试题库 >

ASP.NET使用SqlConnection连接MySQL

吕俊美
2023-03-14
问题内容

这是保存在中的连接字符串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数据库?


问题答案:

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



 类似资料:
  • 题目中的问题相当简单。internet上的所有教程都讨论了OpenID Connect在。NET核心中的实现。我目前的项目是在ASP.NET MVC(不是ASP.NET核心)中开发的,我需要在其中实现OpenID Connect。 我跟踪这个帖子,并试图,但没有运气! 任何关于这方面的帮助/澄清将不胜感激。

  • 问题内容: 我想在最后关闭SqlConnection,因为使用并没有真正关闭它,并且连接池已满。但我不知道该怎么做才是正确的方法,因为在最后一节中再也无法访问conn对象了。 问题答案: 扩展为: 因此,您应该处理错误,但是您会忘记关闭连接。

  • 问题内容: 我以为我很聪明。但是鉴于最近的发现,我不确定。在页面生命周期中,可能有许多数据库交互。有些背靠背,有些则散布开来。因此,我发明了一个对象,该对象在HttpContext.Items字典中使SQL连接的实例保持活动状态。然后,每个数据库请求都使用此连接,并且当http请求结束时,我会正确处理该连接。我们正在等待几百毫秒的时间,连接将打开,并且通过大量的HTTP高速缓存,可用连接的用尽也不

  • 我有一个来自ASP.NET的连接字符串,我必须在我的Java应用程序中使用它,然而,我似乎无法弄清楚它为什么不工作。 这是我得到的错误:cannot ;建立 ;连接 ;到 ;jdbc:sqlserver://localhost:1433;databaseName=mydatabase;“ 使用 ;com.microsoft.sqlserver.jdbc.sqlserverdriver ;( ;连接

  • 本文向大家介绍sqlalchemy 使用连接,包括了sqlalchemy 使用连接的使用技巧和注意事项,需要的朋友参考一下 示例 您可以使用上下文管理器打开连接(即从池中请求一个连接): 有无,但必须手动将其关闭:            

  • 我正在用vaadin和spring开发一个Web应用程序java,比如容器和eclipse链接,比如持久化框架。现在我想在我的应用程序中使用一个连接池。我在谷歌上读了一些我还没读到的东西。这是我的应用程序配置: 我的春日文脉 我的persistence.xml 但我得到以下异常: