【发布时间】:2015-11-04 04:54:17
【问题描述】:
在 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" />
在控制器类中:
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
谁能指出我的代码有什么问题。
【问题讨论】:
-
当您使用
using语句时,您不需要显式关闭您的连接! -
好的。但它仍然给我一个错误 conn.open()
标签: mysql asp.net-mvc exception web-config