在 Windows 窗体应用程序中没有 web.config。那么如何声明单个连接字符串呢?我如何在其他页面中调用它?
在App.config页面
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="CONSTRING" connectionString="Data Source=SQL-PC;Initial Catalog=DATABASE;Integrated Security=True"/>
</connectionStrings>
</configuration>
在 Form 中我称之为连接字符串。
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace Sample {
public partial class Sample: Form
{
public string conn = ConfigurationManager.ConnectionStrings["CONSTRING"].ConnectionString;
}
现在有错误显示。 ConfigurationManager
在此当前上下文中不存在。我该如何解决?
最佳答案
ConfigurationManager
类驻留在 System.Configuration
程序集中。为了使您的代码正常工作,您需要将对 System.Configuration
程序集的引用添加到项目中。
关于c# - 表单应用中连接字符串的单一声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17905585/