ExecuteReader: Connection 属性尚未初始化
web.config 文件的代码
<appSettings>
<add key="conn" value="Data Source=WTX-PC;Initial Catalog=MIMS_database;Initial Catalog=pubs;Integrated Security=True"/>
</appSettings>
<connectionStrings>
<add name ="conn" connectionString="server='(local)';database='MIMS_database';uid='sa';pwd='111'"/>
</connectionStrings>
登陆页面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String connStr = ConfigurationManager.AppSettings["conn"];
SqlCommand com= new SqlCommand("select * from Administrator where UserName='" + TextBox1.Text.ToString() + " 'and PassWord='" + TextBox2.Text.ToString() + "'and Authority=1,conn");
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
dr.Close();
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "sd", "alert('登录成功!')", true);
Session["UserName"] = TextBox1.Text.ToString();
Response.Redirect("OUT.aspx");
}
else
{
dr.Close();
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "sd", "alert('用户名或密码错误!')", true);
TextBox1.Text = "";
TextBox2.Text = "";
}
}
问题:ExecuteReader: Connection 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化。
源错误:
行 22: SqlCommand com= new SqlCommand("select * from Administrator where UserName='" + TextBox1.Text.ToString() + " 'and PassWord='" + TextBox2.Text.ToString() + "'and Authority=1,conn");
行 23:
行 24: SqlDataReader dr = com.ExecuteReader();
行 25:
行 26: if (dr.Read())
源文件: d:\WebSite\Login.aspx.cs 行: 24
堆栈跟踪:
[InvalidOperationException: ExecuteReader: Connection 属性尚未初始化。]
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +5287209
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +82
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
System.Data.SqlClient.SqlCommand.ExecuteReader() +99
Login.Button1_Click(Object sender, EventArgs e) in d:\WebSite\Login.aspx.cs:24
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
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) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
相关推荐
C#使用ExecuteReader返回DataReader
例如:```csharpSqlCommand command = new SqlCommand("YourProcedureName", connection);command.CommandType
Godot Mono C# SQLite数据库读写与安卓兼容性实战指南
在游戏与应用开发中,数据持久化是核心需求,而SQLite作为一款轻量级、零配置的嵌入式关系型数据库,因其单文件、跨平台的特性,成为本地数据存储的理想选择。其工作原理基于将数据存储在单一磁盘文件中,并通过SQL引擎提供完整的ACID事务支持,无需独立的服务器进程。这一技术价值在于为开发者提供了高效、可靠且易于集成的结构化数据管理方案,尤其适用于需要离线存储、复杂查询和排序分页的场景,如游戏存档、配置管理和工具应用。在Godot引擎的C#(Mono)开发环境中,实现SQLite的稳定读写并确保跨平台(特别是安卓
ExecuteNonQuery();ExecuteReader();ExecuteScalar();的简单用法
(); using(SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string firstName
ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别
**示例**:```csharpusing(SqlConnection con = new SqlConnection("your_connection_string")){ con.Open(); using
C#操作sql server数据库.docx
SqlCommand提供了四个执行方法:ExecuteNonQuery、ExecuteScalar、ExecuteReader和ExecuteXmlReader。


