问一个数据库的连接问题
我用vs2005的建立了数据库连接,在web.config中生成了如下代码:
<configuration>
<appSettings/>
<connectionStrings>
<add name= "conn " connectionString= "Data Source=COMPUTER\SQLEXPRESS;Initial Catalog=login;User ID=sa;Password=44116501 "
providerName= "System.Data.SqlClient " />
</connectionStrings>
我在其他页引用的时候 用如下代码:
SqlConnection aconn = new SqlConnection(ConfigurationManager.AppSettings.Get( "conn "));
aconn.Open();
string sql1 = "select count(*) from login ";
SqlCommand command = new SqlCommand(sql1, aconn);
command.ExecuteNonQuery(); //执行查询,返回成功或者失败。
提示:ConnectionString 属性尚未初始化
请问改如何写 谢谢
------解决方案--------------------ConfigurationManager.ConnectionStrings[ "conn "]
------解决方案--------------------string connString = WebConfigurationManager.ConnectionStrings[ "conn "].ConnectionString;
------解决方案--------------------SqlConnection aconn = new SqlConnection(ConfigurationSettings.AppSettings[ "conn "]);
------解决方案--------------------是呀!同意楼上几位所说的。就用一些标准的写法就OK了啊!不用太那个的啊!是吧?
虽然我没有帮楼主解决此问题,但是我帮楼主顶,让更多的人来帮你解决这个问题,这个问题应该不难解决的,挺简单的。
------解决方案--------------------样例代码:
public DataSet GetDataSet()
{
SqlConnection conn = WebConfigurationManager.ConnectionStrings[ "conn "].ConnectionString;
string sqlstr= "select * from table1 ";
SqlCommand command = new SalCommand(sqlstr, conn);
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
adapter.Fill(ds, "aaa ");
return ds;
}