2016-03-21 17 views
0

Ich mache eine C# -Anwendung mit 2 Windows Forms und einer Datenbankverbindung. Die Verbindungszeichenfolge zu dieser Datenbank wird auf den Code verwiesen, aber ich möchte es nicht.Verbindungszeichenfolge außerhalb des Codes

Ich möchte wissen, wie man diese Verbindungszeichenfolge außerhalb meines Codes behält. Wenn sich also etwas ändern muss, geschieht diese Änderung nur außerhalb des Programms.

Hoffe ich machte mich klar.

Für jede Hilfe geschätzt.

+0

was über den Code in der 'App, config' Datei setzen? Oder willst du es weiter weg vom Code? –

Antwort

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: 

enter image description here Schritt 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". 

enter image description here

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. 

enter image description here dann in dem Programm, den Zugang (USE) der connecti Ein String mit: Properties.Setting.Default. (Name) Dabei steht (Name) für den Namen, den Sie in der Namensspalte angegeben haben.

Example 

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

Coding 

Form1.cs[Design] 

enter image description here

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 
+0

Ref: http: //www.c-sharpcorner.com/UploadFile/5089e0/create-single-connection-string-for-all-windows-form-in-net/ –

Verwandte Themen