ASP.NET use SqlConnection connect MySQL
Asked Answered
L

3

9

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;
}

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)

Is that possible SqlConnection to connect MySQL database?

La answered 15/8, 2013 at 12:58 Comment(2)
have you looked at connectionstrings.com/mysqlTonjatonjes
Had the same problem and these are the 2 links help me: To check that MySQL configuration is correct followed instructions on page MySQL EF6 Support Was using MS SQL and MySQL in the same project, then had to add a DBConfiguration as explained in: DBConfiguration for MS SQL and MySQLVladamar
H
28

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);

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

Hipparch answered 15/8, 2013 at 13:0 Comment(1)
JDBC is better.Arrow
D
4

as Darren seid "SqlConnection is for SQL Server." you need to install MySql.Data from NuGet: mySql.Data

also you can use Install-Package MySql.Data in your Package Manager Console

then you can create MySqlConnection object and connect to your database:

var cnn = new MySqlConnection("my Connection String");
Diametrically answered 2/11, 2019 at 7:28 Comment(0)
F
0

Not that I know of, and even if it would, why would you want to? You are using a connection object specifically created for Microsoft SQL Server, so it wouldn't connect in the same fashion MySQL does.

For accessing a MySQL database, you should use the MySQL .NET connector which you can find here.

Furred answered 15/8, 2013 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.