我正在使用2个Windows窗体和数据库连接执行C#应用程序。代码中引用了该数据库的连接字符串,但我不希望它。
我想知道如何将该连接字符串保留在我的代码之外,因此如果必须更改某些内容,则该更改仅发生在程序之外。
希望我清楚自己。
感谢您的任何帮助。
答案 0 :(得分:0)
您应该使用App.config定义连接字符串。如果您需要调用用户凭据,请务必按照文章中的说明加密连接字符串。
https://msdn.microsoft.com/en-us/library/ms254494%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
答案 1 :(得分:0)
step 1
Just go to the "Properties" node then double-click on "Settings.Settings". You will then get a grid with columns for Name, Type, Scope and Value as in the following:
Now, specify a name, then click in the Type cell. In the drop-down box choose "Connection String". Then set the Scope to "Application".
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.
然后在程序中,使用: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
[![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