简体   繁体   English

如何编写外部连接字符串配置文件并使我的C#应用​​程序识别并读取它?

[英]How do I write an external connection string config file and make my c# application recognize and read it?

My current c# windows form application is using the following code to connect to a database to retrieve information and the it is stored at Setting.cs 我当前的c#Windows窗体应用程序正在使用以下代码连接到数据库以检索信息,并将其存储在Setting.cs中

public static String connectionString ="Data Source=####; Initial Catalog=###; User ID=####; Password='####'";

I have written out an external Connection.config file and the content would be : 我已经写了一个外部Connection.config文件,其内容为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Connection" providerName="System.Data.providername" 
         connectionString="Data Source=####; Initial Catalog=###; User ID=###;                      Password='####!';" />
  </connectionStrings>
</configuration>

is the above code written correctly? 上面的代码编写正确吗? as I sort of just copied and paste online and editted the connection string part as i do not know what does the "add name" and providerName part does. 因为我只是在线复制和粘贴并编辑了连接字符串部分,因为我不知道“添加名称”和providerName部分的作用。

what i would like to ask is where should i write the code in my forms to read the connection string from my external config file so i can remove the one in my setting.cs file. 我想问的是,我应该在哪里在窗体中编写代码以从外部配置文件中读取连接字符串,以便可以在我的setting.cs文件中删除该代码。

thank you. 谢谢。

don mind the ### as it is just replaced by me to represent the information. 不要介意###,因为它只是由我代替以表示信息。

You can use config source to split your config files. 您可以使用配置源拆分配置文件。 Example App/web.config: 示例App / web.config:

<connectionStrings configSource="myExternalConfigSource.xml" />

External Config: 外部配置:

<connectionStrings>          
  <add name="Connection" providerName="System.Data.providername" 
     connectionString="Data Source=####; Initial Catalog=###; User ID=###; />
</connectionStrings>  

More info: http://www.nikhilk.net/Entry.aspx?id=158 更多信息: http : //www.nikhilk.net/Entry.aspx?id=158

If you use this method, you can reference your config as normal and still have your configuration split out into seperate files. 如果使用此方法,则可以照常引用配置,并且仍然可以将配置拆分为单独的文件。

we use: 我们用:

<configuration>
    <appSettings>
        <add key="connection.string" value="Initial Catalog=XXX;server=localhost;User=YYY;Password=ZZZ;" />
    </appSettings>
</configuration>

and then just: 然后:

ConfigurationManager.AppSettings["connection.string"];

Add the following in your web.config file right at the bottom above the closing config tag. 将以下内容添加到您的web.config文件中,位于关闭config标记上方的底部。

   <connectionStrings>
      <add connectionString="Data Source=Servernamegoeshere;Initial Catalog=databasenamehere;Persist Security Info=True;User ID=***;Password=***" name="nameyouwanttogivethisconnection" providerName="System.Data.SqlClient" />
   </connectionStrings>
</configuration>

To reference the connection on the page load of the main form use this: 要在主窗体的页面加载中引用连接,请使用以下命令:

public void Page_Load(object sender, EventArgs e)
{
        string connectionString = ConfigurationManager.ConnectionStrings["nameyouwanttogivethisconnection"].ConnectionString;
        SqlConnection SqlConnection = new SqlConnection(connectionString);
        SqlCommand SqlCommand = new SqlCommand("update table etc etc....",SqlConnection);
        SqlConnection.Open();
        SqlCommand.ExecuteNonQuery(); //This line is for updates and inserts, use SqlCommand.ExecuteReader(CommandBehavior.CloseConnection); for select statments
        SqlConnection.Close();
}

Make sure you add these references to the top of the page you are using this code on: 确保将这些引用添加到在以下位置使用此代码的页面顶部:

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

You may then use that connection to create a Sqlcommand to run against your database. 然后,您可以使用该连接来创建对数据库运行的Sqlcommand。

You could also use ApplicationSettings, and forget about writing information in config files manually (It will be done automatically) 您也可以使用ApplicationSettings,而不必手动在配置文件中写入信息(这将自动完成)

  1. Right click on your project in solution explorer and select Properties. 在解决方案资源管理器中右键单击您的项目,然后选择“属性”。
  2. Go to Settings tab. 转到设置标签。
  3. Click on the link to create the settings file. 单击链接以创建设置文件。 (It will be called Settings.settings) (这将称为Settings.settings)
  4. For Name, type the name you want to use for the connection string. 对于“名称”,键入要用于连接字符串的名称。 (ie ConnectionString) (即ConnectionString)
  5. For Type, select Connection String 对于类型,选择连接字符串
  6. For Scope, select Application 对于范围,选择应用
  7. For Value paste your connection string. 对于值,请粘贴您的连接字符串。

When you want to use it, just type 当您要使用它时,只需键入

Application.Settings.Default.ConnectionString

ie

SqlConnection connection = new SqlConnection(Application.Settings.Default.ConnectionString);

暂无
暂无

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

相关问题 在C#中,如何读取存储在web.config文件连接字符串中的连接字符串? - In C# , how can I read a connection string stored in my web.config file connection string? 如何将 C# 字符串字典读取和写入文件? - How do I read and write a C# string Dictionary to a file? 如何创建非绝对连接字符串,以便我的小型数据库可以与已发布的C#应用​​程序一起使用? - How do I make a non absolute connection string so my small database works with my published C# application? 如何找到通过C#应用程序自动执行SAP所需的连接字符串? - How do I find my Connection String required to automate SAP through a C# application? 如何在 web.config 文件中写入连接字符串并从中读取? - How to write connection string in web.config file and read from it? C# - 如何读写二进制文件? - C# - How do I read and write a binary file? 如何在C#中向文本文件读取和写入日期 - How do I read and write a date to a text file in C# 如何从文件中读取/写入 C# BigIntegers? - How do I read/write C# BigIntegers to/from a file? 如何通过C#从App.config文件中读取连接字符串 - how to read the connection string from App.config file by C# 当我在客户端PC中安装连接字符串时,我需要在c#应用程序中编辑连接字符串吗,如果可以,我该怎么做? - do i need to edit connection string in my c# application when i install it in a client pc, if yes how can i do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM