在Asp.net mvc中,我试图打开一个sql连接,但它总是给我无效的连接致命错误。 这是我的以下代码:
在Web.config中:
<connectionStrings>
<add name="test" connectionString="Data Source=test,3306;Initial catalog=test;User Id=xxxxx;password=xxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
在Controller类中:
var sql = "select * from users where email = \"" + model.Email + "\" and password = \"" + model.Password +\"";
System.Diagnostics.Debug.WriteLine("return url = "+sql);
string connectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (var conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
System.Diagnostics.Debug.WriteLine("output = " + dr.HasRows);
conn.Close();
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine("exception " + e.ToString());
}
}
它在conn.open();
上给出了以下异常 System.InvalidOperationException: Internal connection fatal error. Error state: 18
任何人都可以在我的代码中提出错误的建议。
答案 0 :(得分:1)
public MySqlConnection GetMySqlConnection(string dataBase, string server,uint port,string userID,string pass)
{
MySqlConnectionStringBuilder myBuilder = new MySqlConnectionStringBuilder();
myBuilder.Database = dataBase;
myBuilder.Server = server;
myBuilder.Port = port;
myBuilder.UserID = userID;
myBuilder.Password = pass;
MySqlConnection myconn = new MySqlConnection(myBuilder.ConnectionString);
myconn.Open();
return myconn;
}
说明强> 您应该使用MySql而不是MS SQL。 安装MySql连接器net和MySql for Visual Studio(“只是google他们”)
安装后:
您必须在参考文献中添加MySql.Data
:使用MySql.Data.MySqlClient;