简体   繁体   English

如何使用 App.config 将 C# 与 Db 连接起来?

[英]How can I connect C# with Db with App.config?

I need to connect C# with SQL Server database using app.config .我需要使用app.config将 C# 与 SQL Server 数据库连接起来。

My code is:我的代码是:

public partial class Form1 : Form
{
    string  connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;     

    public Form1()
    {
            InitializeComponent();
    }

    public void InsertDeleteUpdate(string query)
    {
            try
            {
                SqlConnection Conn = new SqlConnection(connectionString);
                Conn.Open();
                SqlCommand comm = new SqlCommand(query, Conn);
                comm.ExecuteNonQuery();
                Conn.Close();
                MessageBox.Show("Succeeded");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);   
            }
        }

and in app.config I have:app.config我有:

<connectionStrings>
    <add name="Test1" 
         providerName="System.Data.SqlClient"
         connectionString="Data Source=KSPR1WS126;Initial Catalog=Test1;User ID=sa" />
</connectionStrings>

but I get an error and I can't fix it:但我收到一个错误,我无法修复它:

ConfigurationErrorExeption was Unhandled Configuration system failed to initialize ConfigurationErrorExeption is Unhandled 配置系统初始化失败

Can anyone help me please ?有人可以帮我吗?

Try:尝试:

var connectionString = ConfigurationManager.ConnectionStrings["Test1"].ConnectionString; 

You're referencing Conn as the connection string name but referenced it as Test1 in the App.Config你引用Conn作为连接字符串名称,但引用它Test1App.Config

尝试下面的代码,然后检查

string connectionString = ConfigurationManager.ConnectionStrings["Test1"].ConnectionString;

Try this..试试这个..

  <connectionStrings>
  <add name="Conn" providerName="System.Data.SqlClient"
        connectionString="Data Source=KSPR1WS126;Initial Catalog=Test1;User ID=sa" />
  </connectionStrings>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用ModuleInfo标签的app.config的情况下使用C#使用Emc.Documentum.FS.Runtime连接到Documentum? - How can I connect to Documentum with Emc.Documentum.FS.Runtime using C# without the app.config for the ModuleInfo tags? 如何使用.Net Framework 4 Client Profile从c#中的App.config加载值 - how can I load values from App.config in c# using .Net Framework 4 Client Profile 如何在C#中从app.config搜索应用程序的版本? - How can I search the Version of the application from app.config in c#? 在使用 C# 测试 Selenium 测试时,如何使用 App.config 存储 url 和检索 url? - How can I use App.config to store urls and retrieve the urls in testing Selenium tests with C#? 如何使用 spring.net 将枚举作为属性传递给 c# 中的 app.config 文件 - how can i pass enum as a property to app.config file in c# with spring.net 如何在C#中更新app.config - How to update app.config in c# 需要通过C#中的app.config文件连接数据库 - Need to connect database through app.config file in C# 如何在c#中的app.config中读取自定义配置部分 - How to read custom config section in app.config in c# C# 库如何检测 app.config 是否存在,如果不存在则手动加载配置文件? - How can a C# library detect if app.config exists and manually load a config file if not? 如何可怕地扭曲ac#项目以读取其类库的app.config文件 - How can I horribly twist a c# project to read its class library's app.config files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM