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
...全文
263 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");
内容概要:本文围绕基于Basisformer模型的时间序列锂离子电池SOC(State of Charge,荷电状态)预测展开研究,利用PyTorch框架实现深度学习模型的构建与训练。通过将历史充放电数据作为输入,Basisformer能够有效捕捉电池状态的动态变化特征,提升SOC预测精度。文中详细介绍了模型结构设计、数据预处理流程、训练策略及实验结果分析,并与传统方法进行对比,验证了该方法在复杂工况下的优越性与鲁棒性。该研究不仅展示了Basisformer在时序建模中的潜力,也为电池管理系统提供了高精度的状态估计解决方案。; 适合人群:具备一定Python编程基础和深度学习理论知识,熟悉PyTorch框架,从事电池管理系统、新能源汽车或智能预测方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于电动汽车、储能系统等领域的电池SOC高精度实时估算;②为电池健康管理(BMS)提供可靠的状态输入;③推动深度学习在时间序列预测中的实际落地,提升现有预测模型的泛化能力与稳定性; 阅读建议:建议读者结合标题为【锂电池SOC估计】【PyTorch】基于Basisformer时间序列锂离子电池SOC预测研究(python代码实现)的资源,重点研读所提供的Python代码,深入理解数据处理方式与模型网络结构的设计思路,尝试调整超参数以观察对预测性能的影响,从而全面掌握Basisformer在时序建模中的优势、适用边界及工程化实现路径。

111,131

社区成员

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

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

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