Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
Database 代码外的连接字符串_Database_Connection String - Fatal编程技术网

Database 代码外的连接字符串

Database 代码外的连接字符串,database,connection-string,Database,Connection String,我在做一个C#应用程序,有两个Windows窗体和一个数据库连接。该数据库的连接字符串在代码中被引用,但我不希望它被引用 我想知道如何将连接字符串保留在代码之外,因此,如果必须更改某些内容,那么更改只会发生在程序之外 希望我说清楚 感谢您的帮助。您应该使用App.config定义连接字符串。如果需要调用用户凭据,请确保按照本文所述加密连接字符串 步骤2 Now, specify a name, then click in the Type cell. In the drop-down box

我在做一个C#应用程序,有两个Windows窗体和一个数据库连接。该数据库的连接字符串在代码中被引用,但我不希望它被引用

我想知道如何将连接字符串保留在代码之外,因此,如果必须更改某些内容,那么更改只会发生在程序之外

希望我说清楚


感谢您的帮助。

您应该使用App.config定义连接字符串。如果需要调用用户凭据,请确保按照本文所述加密连接字符串

步骤2

Now, specify a name, then click in the Type cell. In the drop-down box choose "Connection String". Then set the Scope to "Application".

然后在程序中,使用:Properties.Setting.Default.(name)访问(使用)连接字符串,其中(name)是您在“名称”列中提供的名称

Example

In this example, we will display a record in a DataGridView on a button click.

Coding

Form1.cs[Design]

Form1.cs
[![在此处输入图像描述][5][5]
App.config
您可以看到,连接字符串将自动包括在内。。
输出
[![在此处输入图像描述][5][5]
[1]: http://i.stack.imgur.com/hyNXF.jpg
[2]: http://i.stack.imgur.com/4BJ5a.jpg
[3]: http://i.stack.imgur.com/F3ViL.jpg
[4]: http://i.stack.imgur.com/oGC5a.jpg
[5]: http://i.stack.imgur.com/OZO22.jpg

将代码放入
应用程序的config
文件怎么样?或者你想让它远离代码?参考:
Step 3

Then click in the value cell and an ellipsis ("...") will appear in the right. Click on the ellipsis and create the connection string.
Example

In this example, we will display a record in a DataGridView on a button click.

Coding

Form1.cs[Design]
Form1.cs

[![enter image description here][5]][5]

App.config

You see that the Connection String is automatically included..

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="ConnectionString.Properties.Settings.connection" connectionString="Data Source=MCNDESKTOP43;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=*****"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>


Output

[![enter image description here][5]][5]



  [1]: http://i.stack.imgur.com/hyNXF.jpg
  [2]: http://i.stack.imgur.com/4BJ5a.jpg
  [3]: http://i.stack.imgur.com/F3ViL.jpg
  [4]: http://i.stack.imgur.com/oGC5a.jpg
  [5]: http://i.stack.imgur.com/OZO22.jpg