ASP.NET有关connectionstring问题
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
string sqlstr = " select * from User where UserName='" + TextBox1.Text + "'and UserPD='" + TextBox2.Text + "'";
string sms_connstr = System.Configuration.ConfigurationManager.AppSettings["sms_dbconn"];
SqlConnection sms_conn = new SqlConnection(sms_connstr);
SqlCommand cmd = new SqlCommand(sqlstr, sms_conn);
sms_conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read() == true)
{
Response.Write("<script>alert('登录成功!');window.location.href='index.aspx';</script>");
sms_conn.Close();
}
else
{
Response.Write(" <script language='javascript'>alert('用户名或密码不正确,请重新输入!');</script>");
}}
这是代码,问题是,一旦我click button,就会提示我没有初始化connectionstring
------解决方案--------------------
ConfigurationManager.AppSettings[0];//AppSettings
ConfigurationManager.ConnectionStrings[0];//ConnectionStrings
一个是AppSettings结点,一个是ConnectionStrings结点,你要看清楚了,你配置的什么节点?
------解决方案--------------------断点看看 sms_connstr 是否正确得到
------解决方案--------------------sms_connstr 有值么
------解决方案--------------------config里面你是怎么配置的?
<connectionStrings>
<add name="strCon" connectionString="Data Source=192.168.10.6;User ID=sa;Password=sa;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>
sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
那么sqlCon 的值就是Data Source=192.168.10.6;User ID=sa;Password=sa;Unicode=True" providerName="System.Data.OracleClient了。
2:
<AppSettings>
<add key="config1" value="aaa" />
</AppSettings>
string str = System.Configuration.ConfigurationManager.AppSettings["config1"].ToString();//str等于aaa