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();
}
}
}
麻烦高手看一下哪里有问题啊
代码没错但是就是每次一运行就出现“未将对象引用设置到对象的实例” 展开
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();
}
}
}
麻烦高手看一下哪里有问题啊
代码没错但是就是每次一运行就出现“未将对象引用设置到对象的实例” 展开
2个回答
展开全部
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();
}
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();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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"]);
改成
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"]);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询