应用程序配置文件:
< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
<connectionStrings>
<add name="dbConnStr" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=MyTest;User ID=sa;Password=123456"/>
</connectionStrings>
< /configuration>
程序:
private void button1_Click(object sender, RoutedEventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
MessageBox.Show(connStr);
}
}
刚开始应用程序配置文件名称为App1.config,出现nullreferenceexception异常:未将对象引用设置到对象的实例。百思不得其解,意外中我重新把名称修改为App.config,就可以运行了,不知为何??? |