mysql sqlconnection,ASP.NET使用SqlConnection連接MySQL

This is the connection string saved in web.config:

這是web.config中保存的連接字符串:

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:

但它無法連接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)

Is that possible SqlConnection to connect MySQL database?

那可能是SqlConnection連接MySQL數據庫嗎?

2 个解决方案

#1

18

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:

SqlConnection適用於SQL Server。您需要MySqlConnection - 這不是.NET Framework的一部分,因此您必須下載它並在項目中引用它。然后,您可以在應用程序中創建MySqlConnection對象並連接到MySQL:

MySqlConnection connection = new MySqlConnection(myConnString);

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

您還必須使用MySqlCommand對象而不是SqlCommand對象。

#2

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.

不是我知道的,即使它會,你為什么要這樣做?您正在使用專門為Microsoft SQL Server創建的連接對象,因此它不會以與MySQL相同的方式連接。

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

要訪問MySQL數據庫,您應該使用可在此處找到的MySQL .NET連接器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值