ExecuteReader: Connection 属性尚未初始化

xinguanxiu 2015-11-17 02:13:52
菜鸟一只,求助各位大神,使用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
...全文
262 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinguanxiu 2015-11-17
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan的回复:
+ "'and Authority=1,conn");
谢谢一楼大神和版主,问题已解决!
xinguanxiu 2015-11-17
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection(); conn.ConnectionString =System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString(); conn.Open(); 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();
  • 打赏
  • 举报
回复
你的SqlConnection 呢? ,如果不知道 去网上搜搜它的用法先。
xdashewan 2015-11-17
  • 打赏
  • 举报
回复
+ "'and Authority=1,conn");
内容概要:本文系统介绍了基于MATLAB平台的风力涡轮机雷达信号仿真研究,涵盖完整的仿真代码、实测数据及配套学术文章,重点实现雷达信号处理、风力涡轮机回波建模、微动特征提取与时频分析等核心技术。通过高精度模拟风机叶片旋转引起的调制效应与雷达散射截面变化,深入揭示风力发电场对周边雷达系统造成的电磁干扰机制,为雷达杂波抑制、目标检测优化及空域兼容性设计提供理论支撑与技术验证平台。资源内容还可拓展应用于无人机路径规划、电动汽车调度、电力系统优化等交叉领域,展现MATLAB在复杂系统仿真中的强大集成能力。; 适合人群:具备MATLAB编程基础与雷达信号处理知识的科研人员,适用于从事新能源并网影响评估、雷达系统设计、电磁兼容分析、智能电网或远程 sensing 领域研究的研究生、工程师及高校教师; 使用场景及目标:①开展风电场对空中监视雷达干扰效应的仿真评估;②研究风电杂波的时频特性与微多普勒特征建模方法;③开发针对非合作目标干扰的雷达抗干扰算法与信号分离技术;④作为科研项目申报、论文复现或工程预研的技术原型工具包; 阅读建议:建议结合百度网盘提供的完整资料包(含代码、数据集与参考文献)同步学习,优先运行示例脚本以掌握仿真流程,再根据具体研究需求调整系统参数进行扩展实验,同时可借鉴文中提到的其他MATLAB仿真案例以提升研究深度与广度。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧