C# ConfigurationManager.ConnectionStrings.ConnectionString 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18218109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:33:10  来源:igfitidea点击:

ConfigurationManager.ConnectionStrings.ConnectionString Issue

c#database-connectionconnection-string

提问by Samuel Brockmann

I am trying to pull in data from a Microsoft Access database file, in order to populate several textboxes. (The textboxes are all done in XAML.) I'm quite sure I'm missing something, because the database file isn't accessed.

我正在尝试从 Microsoft Access 数据库文件中提取数据,以填充多个文本框。(文本框都是在 XAML 中完成的。)我很确定我遗漏了一些东西,因为没有访问数据库文件。

Here is my code:

这是我的代码:

    DataTable tblVFWPostManagers = new DataTable();
    string connString2 = ConfigurationManager.ConnectionStrings**/*["\Documents\DatabaseFile.accdb"]*/**.ConnectionString;
    string query2 = @"SELECT Manager ID, Manager FName, Manager LName, Manager Address, Manager City, Manager State, Manager Zip Code,
            Manager Home Phone Number, Manager Cell Phone Number, Manager Email FROM tblVFWPostManagers";

        //Fill the VFWPostManagers Set with the data
        using (SqlConnection conn2 = new SqlConnection(connString2))
        {
            SqlDataAdapter da2 = new SqlDataAdapter(query2, conn2);
            da2.Fill(tblVFWPostManagers);
        }

Note: I'm sure the bolded is incorrect. However, I'm not really sure what goes in those brackets. I assumed, at first, that it was where the filepath went. When I commented that section out, the error disapperead though.

注意:我确定粗体是不正确的。但是,我不确定这些括号中的内容。起初,我认为这是文件路径所在的位置。当我注释掉该部分时,错误消失了。

How can I pull in the data from my database using the above method? What am I missing?

如何使用上述方法从我的数据库中提取数据?我错过了什么?

回答by Jonesopolis

In your app.config or web.config file you have a ConnectionStrings section :

在您的 app.config 或 web.config 文件中,您有一个 ConnectionStrings 部分:

<configuration>
    <connectionStrings>
        <add name="MyConnection" connectionString="..."/>
    </connectionStrings>
    ...
</configuration>

You can access it in your code :

您可以在代码中访问它:

string connString2 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

回答by Steve

A couple of errors in your code:

您的代码中有几个错误:

ConfigurationManager.ConnectionStringsreferences a specific section of your app config where are stored the informations to access your databases (one or more). The Section contains lines like these

ConfigurationManager.ConnectionStrings引用您的应用程序配置的特定部分,其中存储了访问您的数据库(一个或多个)的信息。该部分包含这样的行

  <connectionStrings>
            <add name="MyDataBase" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
                                   Data Source=C:\myFolder\myAccess2007file.accdb;
                                   Persist Security Info=False"/>
  </connectionStrings>

(To create a valid connectionstring for your app look at www.connectionstrings.com)
So your code refers to these section voices using the 'name' key with

(要为您的应用程序创建有效的连接 字符串,请查看www.connectionstrings.com
因此您的代码使用“名称”键引用这些部分语音

string connString2 = ConfigurationManager.ConnectionStrings["MyDataBase"].ConnectionString;

Said that now the text of your query will fail because you use extensively columns names with spaces. In this case every column name should be enclosed in square brackets.

说现在您的查询文本将失败,因为您广泛使用带空格的列名。在这种情况下,每个列名都应该用方括号括起来。

string query2 = @"SELECT [Manager ID], [Manager FName], [Manager LName], .....