我使用的是ASP.NET MVC 4和C#,当我调用SqlConnection
构造函数时,它需要很长时间才能进入下一条指令。我尝试用连接字符串创建对象,没有它并设置超时但它仍然需要相同的时间。
以下是一些代码......
string dbConn = ConfigurationManager.ConnectionStrings["sqlServer2012_2"].ToString();
string query = "SELECT * FROM testTable";
List<carTestModel> model;
//MessageBox.Show(dbConn);
try {
SqlConnection sqlConn = new SqlConnection(); //HERE IS WHERE IT TAKES AGES
sqlConn.ConnectionString = dbConn;
using(sqlConn) {
sqlConn.Open();
SqlCommand cmd = new SqlCommand(query, sqlConn);
SqlDataReader rdr = cmd.ExecuteReader();
model = new List<carTestModel>();
while(rdr.Read()) {
var testRow = new carTestModel();
testRow.id = (int)rdr["carIdTest"];
testRow.name = (string)rdr["carNameTest"];
model.Add(testRow);
}
}
MessageBox.Show("Done!");
}
catch(SqlException exc) {
MessageBox.Show("Fail! " + exc.Message);
}
感谢您的阅读。