Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

 

How to determine the Connectionstring to bind a local database in Windows Forms Winforms

 

Location Connectionstring:

Where can I find the ConnectionString of the local database in Windows Forms?

The Connectionstring is stored in the App.Config as an XML structure

 

The completed App.config in Winforms looks like this:

This contains a node with the name connectionstrings. In this, a reference to a database is created with add.

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <configSections>

    </configSections>

    <connectionStrings>

        <add name="webrobot_ebayKleinanzeigen.Properties.Settings.dbRobotConnectionString"

            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\data\dbRobot.mdf;Integrated Security=True"

            providerName="System.Data.SqlClient" />

    </connectionStrings>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>

    </startup>

</configuration>

 

 

With providerName = SqlClient, the database is automatically assigned to a SQL Server.

That is, when you are requesting or sending data from the database, the data is processed on the same computer through a SQL server.

<add name="webrobot_ebayKleinanzeigen.Properties.Settings.dbRobotConnectionString"

 connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\data\dbRobot.mdf;Integrated Security=True"

 providerName="System.Data.SqlClient"

/>

 

 

2. Settings

By the designation <add ..> the connection string is automatically copied or mapped to the settings of the application.

In the settings the value is also direct as connectionstring.

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\data\dbRobot.mdf;Integrated Security=True

 

Settings in Visual Studio

 

application

Access the Connectionstring

 

2a) With System.Configuration

You can connect the current Connectionstring with 2 ways.

Connection with System.Configuration

 

To do this, you must add the reference System.Configuration

 

private void load_data()

{

string cn_string = System.Configuration.ConfigurationManager.ConnectionStrings["webrobot_ebayKleinanzeigen.Properties.Settings.dbRobotConnectionString"].ConnectionString;

 

SqlConnection cn = new SqlConnection(cn_string);

}

 

 

2a) With Settings

It is easier to use Properties.Settings.Default. the current Connectionstring settings

 

//via app-Settings

string cn_string = Properties.Settings.Default.app_ConnectionString;

//</ init >

..

SqlConnection cn = new SqlConnection(cn_string);

if (cn.State != ConnectionState.Open) cn.Open();

..

 

 

#Winforms Database Connection String Connectionstring LocaleDB

 

Mobile
»
C #: Show picture in WinForms
»
SMTP Error: Unable to read data from the transport connection
»
SMTPClient for sending with SSL and Authentication
»
SendMail Client in .Net Winform, WPF
»
Winforms: Connectionstring to the database
»
Net WinForms: Screen to Bitmap
»
WinForms: Code: Change the resolution of the monitor by app
»
C#, WinForms: Save and read settings

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de