我的数据库连接哪有错呢?
WEBCONFIG里的连接字符串
<connectionStrings>
<add name= "abc " connectionString= "Data Source=HINJX3COJZB09GL;Initial Catalog=duke;Integrated Security=True " providerName= "System.Data.SqlClient "/>
</connectionStrings>
后台连接:
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings[ "abc "]);
conn.Open();
SqlCommand cmd = new SqlCommand( "SELECT * FROM duke ", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
提示是: conn.Open()未初始化. 请问是不是因为数据库没连接通?
------解决方案--------------------ConfigurationManager.AppSettings[ "abc "]);
这个是读AppSettings中的设置。而你是放在connectionStrings下了,而且写法也不对。
------解决方案--------------------ConfigurationManager.ConnectionStrings
------解决方案-------------------- <appSettings>
<add key= "ConnectionString " value= "server=.;database=Test;uid=sa;pwd=; "> </add>
</appSettings>
string conn= System.Configuration.ConfigurationSettings.AppSettings[ "ConnectionString "];
conn.Open();
SqlCommand cmd = new SqlCommand( "SELECT * FROM duke ", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();