获得在C#中的app.config中的ConnectionString [英] Get ConnectionString from app.config in c#

查看:823
本文介绍了获得在C#中的app.config中的ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app.config文件中定义我的连接字符串

I have defined my connection String in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>

    <add name="CString" 
         connectionString="Data Source=Bilal-PC;Initial Catalog=ATMSoftware;Integrated Security=False; User Id=sa; Password=123" 
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

现在我想要得到它变成我的C#类,我已经尝试了所有的的methods 。但我正在逐渐的配置管理器错误。

Now I want to get it into my C# class and I have tried all the methods. but I am getting an error on configuration manager.

请帮我。

推荐答案

确保您已在您的项目加入到System.Configuration参考,并放置一个

Ensure that you have added a reference to System.Configuration in your project, and place a

using System.Configuration;

语句。在 ConfigurationManager中键入现在应该是可用的。

statement at the top of your source. The ConfigurationManager type should now be available.

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

这篇关于获得在C#中的app.config中的ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆