首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlDataReader错误

SqlDataReader错误
EN

Stack Overflow用户
提问于 2013-09-06 01:20:51
回答 2查看 3K关注 0票数 1

我试图使SqlParameter和当我运行网站错误出现在SqlDataReader _dataReader = _command.ExecuteReader();

这是我的代码:

代码语言:javascript
复制
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属性尚未初始化。

源错误:

代码语言:javascript
复制
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

堆栈跟踪:

代码语言:javascript
复制
[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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-06 01:29:05

您必须给出SqlCommand的连接名。

使用SqlCommand _command = new SqlCommand(_sqlCodes, conn);

票数 1
EN

Stack Overflow用户

发布于 2013-09-06 01:25:37

SqlCommand对象不知道要使用哪个连接。您可以使用以下方法创建命令:

代码语言:javascript
复制
SqlCommand _command = conn.CreateCommand();

或在其上设置连接:

代码语言:javascript
复制
_command.Connection = conn;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18648411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档