- <span style='font-family: 宋体; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;'><font color="#000000">应用程序配置文件:</font></span>
- <?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>
- 程序代码:
- <div class="blockcode"><blockquote> private void button1_Click(object sender, RoutedEventArgs e)
- {
- string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
-
- DataSet dataset = new DataSet();
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- MessageBox.Show("连接成功");
- }
- }
复制代码 产生NullReferenceException异常:未将对象引用设置到对象的实例。找了半天不知错在哪?
|