ExecuteReader: Connection 属性尚未初始化

xinguanxiu xinguanxiu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
菜鸟一只,求助各位大神,使用web.config配置文件 调用SQL 数据库,出现 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
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
xinguanxiu
C# 111,128 成员 642,536 内容