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
...全文
172 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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");

111,097

社区成员

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

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

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