我正在尝试制作 SqlParameter 并且当我运行网站时出现错误SqlDataReader _dataReader = _command.ExecuteReader();
这是我的代码:
private void goLogin()
{
conn.ConnectionString = _connectionString;
conn.Open();
string username = txt_username.Text;
_sqlCodes = "SELECT ACC_PASS,ACC_ID" +
"FROM ACC WHERE ACC_USER = @username";
SqlCommand _command = new SqlCommand(_sqlCodes);
_command.Parameters.Add(new SqlParameter("@username",username));
SqlDataReader _dataReader = _command.ExecuteReader();
//more codes here...
conn.Close();
}
错误:
“/”应用程序中的服务器错误。
ExecuteReader:连接属性尚未初始化。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息: System.InvalidOperationException:ExecuteReader:连接属性尚未初始化。
源错误:
Line 31: SqlCommand _command = new SqlCommand(_sqlCodes);
Line 32: _command.Parameters.Add(new SqlParameter("username",username));
Line 33: SqlDataReader _dataReader = _command.ExecuteReader();
Line 34: conn.Close();
Line 35: }
源文件: C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs 行:33
堆栈跟踪:
[InvalidOperationException: ExecuteReader: Connection property has not been initialized.]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +5089605
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteReader() +89
GoWireless.WebForm1.goLogin() in C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs:33
GoWireless.WebForm1.btn_login_Click(Object sender, EventArgs e) in C:\Users\arnie.mateo\Desktop\GoWireless Project Leonel\GoWireless\GoWireless\account.aspx.cs:52
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
版本信息: Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.272