2013-08-15 15 views
6

Jest to ciąg połączenia zapisane w web.config:ASP.NET stosowanie SqlConnection połączyć MySQL

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

Jest to kod, aby połączyć się z bazą danych:

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 [email protected] and [email protected]"; 
    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; 
} 

jednak nie trafia do żadnego z MySQL, z tym błędem powrotu:

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) 

Czy to możliwe SqlConnection do połączenia bazy danych MySQL?

+0

czy obejrzałeś http://www.connectionstrings.com/mysql/ – Jonesopolis

+0

Gdyby ten sam problem i te 2 linki pomogły mi: Aby sprawdzić, czy konfiguracja MySQL jest poprawna, postępuj zgodnie z instrukcjami na stronie [Wsparcie dla MySQL EF6] (https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html) Używał MS SQL i MySQL w tym samym projekcie, następnie musiał dodać konfigurację DBC, jak wyjaśniono w: [ DBConfiguration dla MS SQL i MySQL] (http://stackoverflow.com/questions/26361592/same-application-different-datodes-entity-framework-6-x-mysql-sql-server/26496907#26496907) – Riga

Odpowiedz

17

SqlConnection jest dla serwera SQL. Potrzebujesz MySqlConnection - to nie jest częścią .NET Framework, więc będziesz musiał download it i odwoływać się do niego w swoim projekcie. Następnie można utworzyć MySqlConnection przedmiotu i podłączyć do MySQL w aplikacji:

MySqlConnection connection = new MySqlConnection(myConnString); 

Będziesz mieć również do korzystania z obiektu MySqlCommand zamiast obiektu SqlCommand.

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

+0

JDBC jest lepszy . – Alex78191

0

że nie wiem, a nawet jeśli miałoby to, dlaczego nie chcesz? Używasz obiektu połączenia specjalnie utworzonego dla serwera Microsoft SQL  , aby nie łączył się w taki sam sposób jak MySQL.

Aby uzyskać dostęp do bazy danych MySQL, należy użyć złącza MySQL .NET, które można znaleźć pod adresem here.