获得从App.config的连接字符串 [英] Get connection string from App.config

查看:165
本文介绍了获得从App.config的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  VAR连接= ConnectionFactory.GetConnection(ConfigurationManager.ConnectionStrings [测试]的ConnectionString,DataBaseProvider。);
 

这是我的App.config中:

< XML版本=1.0编码=UTF-8&GT?; <结构>     <的ConnectionStrings>         <添加名称=测试的connectionString =数据源=;初始目录= OmidPayamak; IntegratedSecurity =真的providerName =System.Data.SqlClient的/>     < /的ConnectionStrings> < /结构>

但我的项目运行时,这是我的错误:

  

对象引用未设置到对象的实例。

解决方案

你就不能做到以下几点:

  VAR连接=
    System.Configuration.ConfigurationManager.ConnectionStrings [测试]的ConnectionString。
 

您组件还需要引用 System.Configuration.dll

var connection = ConnectionFactory.GetConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString, DataBaseProvider);

And this is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;IntegratedSecurity=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

But when my project runs this is my error:

Object reference not set to an instance of an object.

解决方案

Can't you just do the following:

var connection = 
    System.Configuration.ConfigurationManager.ConnectionStrings["Test"].ConnectionString;

Your assembly also needs a reference to System.Configuration.dll

这篇关于获得从App.config的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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