Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从App.config获取连接字符串_C#_Ado.net_Exception Handling_Connection String_App Config - Fatal编程技术网

C# 从App.config获取连接字符串

C# 从App.config获取连接字符串,c#,ado.net,exception-handling,connection-string,app-config,C#,Ado.net,Exception Handling,Connection String,App Config,这是我的App.config: 但当我的项目运行时,这是我的错误: 对象引用未设置为对象的实例 您是否尝试过: var connection = ConnectionFactory.GetConnection( ConfigurationManager.ConnectionStrings["Test"] .ConnectionString, DataBaseProvider); 您是否尝试过: var connection = ConnectionFactory.GetCo

这是我的App.config:


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

对象引用未设置为对象的实例

您是否尝试过:

var connection = ConnectionFactory.GetConnection(
    ConfigurationManager.ConnectionStrings["Test"]
    .ConnectionString, DataBaseProvider);
您是否尝试过:

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

你不能只做以下几件事:

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

您的程序集还需要对
System.Configuration.dll

的引用。您不能只执行以下操作:

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

您的程序集还需要对
System.Configuration.dll

的引用。请检查您的引用下是否包含
System.Configuration
dll。如果没有它,您将无法访问System.Configuration命名空间中的
ConfigurationManager
类。

还要检查您的引用下是否包含
System.Configuration
dll。没有它,您将无法访问System.Configuration命名空间中的
ConfigurationManager
类。

1)创建新表单并添加以下内容:

var connection = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["Test"].ConnectionString;
然后,当您要使用连接时,请在您的表单中执行以下操作:

Imports System.Configuration
Imports Operaciones.My.MySettings

Public NotInheritable Class frmconexion

    Private Shared _cnx As String
    Public Shared Property ConexionMySQL() As String
        Get
            Return My.MySettings.Default.conexionbd
        End Get
        Private Set(ByVal value As String)
            _cnx = value
        End Set
    End Property

End Class
就这样。你将连接到数据库,可以做一些事情

这是针对vb.net的,但逻辑是相同的

1)创建一个新表单并添加以下内容:

var connection = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["Test"].ConnectionString;
 Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
然后,当您要使用连接时,请在您的表单中执行以下操作:

Imports System.Configuration
Imports Operaciones.My.MySettings

Public NotInheritable Class frmconexion

    Private Shared _cnx As String
    Public Shared Property ConexionMySQL() As String
        Get
            Return My.MySettings.Default.conexionbd
        End Get
        Private Set(ByVal value As String)
            _cnx = value
        End Set
    End Property

End Class
就这样。你将连接到数据库,可以做一些事情


这是针对vb.net的,但逻辑是相同的

由于这是一个非常常见的问题,我准备了一些VisualStudio中的屏幕截图,以便在4个简单步骤中轻松完成

 Private Sub frmInsert_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim cn As New MySqlConnection(frmconexion.ConexionMySQL)
cn.open()
string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;

由于这是一个非常常见的问题,我在Visual Studio中准备了一些屏幕截图,以便于通过4个简单步骤进行操作

string sTemp = System.Configuration.ConfigurationManager.ConnectionStrings["myDB In app.config"].ConnectionString;

您可以使用下面的代码行获取连接字符串-

string str = Properties.Settings.Default.myConnectionString; 
以下是参考资料:

您可以使用下面的代码行获取连接字符串-

string str = Properties.Settings.Default.myConnectionString; 
以下是参考资料:

问题似乎与引用无关,您得到的连接字符串为null,因此请确保您已将该值添加到正在运行的项目的配置文件中,这意味着首先启动/执行的主程序/库。

问题似乎与引用无关,您获得的connectionstring为null,因此请确保您已将该值添加到正在运行的项目的配置文件中,即首先启动/执行的主程序/库。

首先在页面中添加对
System.Configuration
的引用

using System; using System.Configuration;

var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
然后根据你的app.config获取连接字符串,如下所示

using System.Configuration;

现在,您手中有了连接字符串,可以使用它了。

首先在页面中添加一个
System.Configuration
的引用

using System; using System.Configuration;

var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
然后根据你的app.config获取连接字符串,如下所示

using System.Configuration;
就这样,现在您手中有了连接字符串,可以使用它了。

试试这个

string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
试试这个

string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();

我引用了
System.Configuration
库,我也有相同的错误。 调试文件没有其app.config,请手动创建此文件。
错误是,我通过复制调试文件夹中的文件“appname.exe.config”解决了这个问题。IDE没有创建该文件。

我引用了
系统配置库,我有相同的错误。
调试文件没有其app.config,请手动创建此文件。

错误是,我通过复制调试文件夹中的文件“appname.exe.config”解决了这个问题。IDE未创建该文件。

此问题中的OP可能试图在dll中使用App.Config

在本例中,代码实际上是试图访问可执行文件的App.Config,而不是dll。由于找不到名称,因此返回Null,因此显示异常

以下帖子可能会有所帮助:
这个问题中的OP可能试图在dll中使用App.Config

在本例中,代码实际上是试图访问可执行文件的App.Config,而不是dll。由于找不到名称,因此返回Null,因此显示异常

以下帖子可能会有所帮助: 这对我很有效:

//Get Connection from web.config file
public static OdbcConnection getConnection()
{
    OdbcConnection con = new OdbcConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    return con;     
}
产出:

数据源=。;初始目录=OmidPayamak;IntegratedSecurity=True“

这对我很有用:

//Get Connection from web.config file
public static OdbcConnection getConnection()
{
    OdbcConnection con = new OdbcConnection();
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    return con;     
}
产出:

数据源=;初始目录=OmidPayamak;集成安全性=True”


首先,您必须向项目添加
System.Configuration
引用,然后使用下面的代码获取连接字符串

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

首先,您必须向项目添加
System.Configuration
引用,然后使用下面的代码获取连接字符串

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

我也有同样的问题。我的解决方案是由两个项目组成的。一个
类库
和一个引用类库项目的网站。问题是我试图访问
类库
项目中的
App.config
,但系统正在网站的
Web.config
中搜索。我将连接字符串放入
Web.config
中,然后。。。问题解决了


主要原因是,尽管在另一个程序集中使用了
ConfigurationManager
,但它正在runnig项目中搜索。

我也遇到了同样的问题。我的解决方案是由两个项目组成的。一个
类库
和一个引用类库项目的网站。问题是我试图访问
类库
项目中的
App.config
,但系统正在网站的
Web.config
中搜索。我将连接字符串放入
Web.config
中,然后。。。问题解决了


主要原因是,尽管在另一个程序集中使用了
ConfigurationManager
,但它正在runnig项目中搜索。

我通过使用索引读取字符串并逐个检查来解决问题。使用名称读取仍会出现相同的错误。
我有