c# - 表单应用中连接字符串的单一声明

标签 c# winforms

在 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/

相关文章:

c# - DataGridView 不需要调用 EndNew

c# - WinForms:进入主菜单后不会触发文本框离开事件

c# - 在窗体之间传递值 (winforms)

c# - 绑定(bind) dataGridView 中的未绑定(bind)列不保存单元格值

c# - 哪个更好,什么时候: using statement or calling Dispose() on an IDisposable in C#?

c# - ExecutenonQuery 不工作

javascript - 服务器端如何获取隐藏字段的值

c# - webBrowser.Navigate 同步

c# - 使用拆分器使 ItemsControl 子项可调整大小

c# - 如何更改 ListView 中的项目索引?