【问题标题】:SqlDataReader ErrorSqlDataReader 错误
【发布时间】:2013-09-10 00:01:44
【问题描述】:

我正在尝试制作 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

【问题讨论】:

  • 你需要 _command.Connection = conn;
  • 谢谢。那个conn >.
  • 没问题。每个人都这样做的次数比他们想的要多。

标签: c# asp.net sql-parametrized-query


【解决方案1】:

您必须将连接名称提供给SqlCommand

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

【讨论】:

    【解决方案2】:

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

    SqlCommand _command = conn.CreateCommand();
    

    或者在上面设置连接:

    _command.Connection = conn;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多