WinForm如何在配置文件中配置数据库连接字符串及正确读取
如提,在WinForm中按照csdn上的说明,总提示缺少引用。。。
App.config:
<connectionStrings>
<add name= "DatabaseConnection "
connectionString= "Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local); "
providerName= "System.Data.SqlClient " />
</connectionStrings>
Form1.cs:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = GetDBConnection();
}
private SqlConnection GetDBConnection()
{
ConnectionStringSettings settings;
settings = ConfigurationManager.ConnectionStrings[ "DatabaseConnection "];
SqlConnection conn = new SqlConnection(settings.ConnectionString);
return conn;
}
}
}
------解决方案--------------------那个System.Configuration是一个单独的程序集, 你给项目添加引用, 会有那个程序集的,他和你引用的程序集命名空间是一样的
------解决方案--------------------没错,应该在引用里独立添加“System.Configuration.dll”