c# vs2010连接SQLServer2008时出现“未将对象引用设置到对象的实例。”

代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;using... 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.IO;

namespace test
{
public partial class teat1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void SqlDataSource1_Selecting1(object sender, SqlDataSourceSelectingEventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

string name;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=userswo-remuhgn;Initial Catalog=myNewsDB;Persist Security Info=True;User ID=myNew;Password=12345"].ToString());
con.Open();
SqlCommand newCom = new SqlCommand("SELECT N_ID,N_name,N_pwd FROM admin",con);
SqlDataReader dr=newCom.ExecuteReader();
dr.Read();
name = dr["N_name"].ToString();
Label1.Text = name;
con.Close();
}
}
}
麻烦高手看一下哪里有问题啊
代码没错但是就是每次一运行就出现“未将对象引用设置到对象的实例”
展开
 我来答
bjbs08
推荐于2016-07-07 · TA获得超过165个赞
知道小有建树答主
回答量:254
采纳率:0%
帮助的人:109万
展开全部
string name;
string Constr=ConfigurationManager.ConnectionStrings["这个是webconfig里的name"].ConnectionString;
SqlConnection con = new SqlConnection(Constr);
con.Open();
SqlCommand newCom = new SqlCommand("SELECT N_ID,N_name,N_pwd FROM admin",con);
SqlDataReader dr=newCom.ExecuteReader();
dr.Read();
name = dr["N_name"].ToString();
Label1.Text = name;
con.Close();
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
正怒月神
2012-06-05 · TA获得超过1157个赞
知道小有建树答主
回答量:710
采纳率:100%
帮助的人:490万
展开全部
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Data Source=userswo-remuhgn;Initial Catalog=myNewsDB;Persist Security Info=True;User ID=myNew;Password=12345"].ToString());
改成
SqlConnection con = new SqlConnection("Data Source=userswo-remuhgn;Initial Catalog=myNewsDB;Persist Security Info=True;User ID=myNew;Password=12345");
或者
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebConfig文件中,对应的Key"]);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式