我试图使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: Connection属性尚未初始化。
源错误:
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 Line: 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版本信息:微软.NET框架版本:4.0.30319;ASP.NET版本:4.0.30319.272
发布于 2013-09-06 01:29:05
您必须给出SqlCommand的连接名。
使用SqlCommand _command = new SqlCommand(_sqlCodes, conn);
发布于 2013-09-06 01:25:37
SqlCommand对象不知道要使用哪个连接。您可以使用以下方法创建命令:
SqlCommand _command = conn.CreateCommand();或在其上设置连接:
_command.Connection = conn;https://stackoverflow.com/questions/18648411
复制相似问题