I'm a beginner in using Asp.net MVC5, I currently doing my project.
我是使用Asp.net MVC5的初学者,我目前在做我的项目。
It referring to "var reader = cmd.ExecuteReader();" Here is my code:
它指的是“var reader = cmd.ExecuteReader();”这是我的代码:
public bool IsValid(string username, string password)
{
string conString = "Server= EJ ; Database=OnlineShopDb ; Integrated Security=SSPI";
SqlCommand cmd = new SqlCommand("Select * from dbo.UserTb where UserName=@UserName and PassWord=@PassWord");
SqlConnection con = new SqlConnection(conString);
cmd.Parameters.AddWithValue("@UserName", SqlDbType.VarChar).Value = username;
cmd.Parameters.AddWithValue("@PassWord", SqlDbType.VarChar).Value = password;
con.Open();
var reader = cmd.ExecuteReader();
if (reader.HasRows)
{
reader.Dispose();
cmd.Dispose();
return true;
}
else
{
reader.Dispose();
cmd.Dispose();
return false;
}
}
1 个解决方案
#1
5
Wrap your connection in a using
statement, and then pass it in as the second parameter of your SqlCommand
constructor:
将您的连接包装在using语句中,然后将其作为SqlCommand构造函数的第二个参数传递:
var query = "Select * from whatever";
using(var con = new SqlConnection(conString))
using(var cmd = new SqlCommand(query, con))
{
con.Open();
// etc
}
#1
5
Wrap your connection in a using
statement, and then pass it in as the second parameter of your SqlCommand
constructor:
将您的连接包装在using语句中,然后将其作为SqlCommand构造函数的第二个参数传递:
var query = "Select * from whatever";
using(var con = new SqlConnection(conString))
using(var cmd = new SqlCommand(query, con))
{
con.Open();
// etc
}