c# vs2010连接SQLServer2008时出现“未将对象引用设置到对象的实例。”
发布网友
发布时间:2022-04-10 04:35
我来回答
共2个回答
热心网友
时间:2022-04-10 06:04
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();
}
热心网友
时间:2022-04-10 07:22
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"]);