Selecting a connection string in a VB.NET application at startup

I have a VB.NET application connecting to SQL Server 2003. There are two databases on the server, MyDatabase and MyDatabase_Test. What I would like to do is show a dialog when the program starts, so that the user can choose which database to use. My idea is to create a new form in the form of a starup form, which sets this property and then runs the main form.

Currently, the connection string is specified in the application configuration file. It would be best if I could specify two different connection strings in this file to choose from, but at the moment this is also acceptable with other solutions, such as hardcoding, with two connecting lines in the launch form.

EDIT: in dataset.xsd seems to be the corresponding part

<Connections>
          <Connection AppSettingsObjectName="MySettings" AppSettingsPropertyName="MyDatabase_ConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyDatabase_ConnectionString(MySettings)" ParameterPrefix="@" PropertyReference="ApplicationSettings.MyProgram.My.MySettings.GlobalReference.Default.MyDatabase_ConnectionString" Provider="System.Data.SqlClient" />
</Connections>

But how do I change it at runtime? The closest I can find is to change which connection is used for each individual TableAdapter, but this does not seem very optimal.

EDIT2: I agree that a modal dialog at startup would be better, but where would I launch it so that it runs before the database connection is initiated?

EDIT3: In the end, I “solved” it by deleting ReadOnly from the settings file. This will be deleted every time the file is automatically generated, so it is not optimal.

EDIT4: The best solution seemed to use a string with a user area instead of a connection string to bind a dataset, and chose a value for this string from two application-related ConnectionStrings.

+3
3

, ReadOnly, .

ConnectionString , . ConnectionString, , IDE Visual Studio.

+1

TableAdapter.Connection.ConnectionString , . app.config, :

<connectionStrings>
      <add name="Live"
        connectionString="Data Source=svr;Initial Catalog=Live;..."
        providerName="System.Data.SqlClient" />
        <add name="Dev"
        connectionString="Data Source=svr;Initial Catalog=Dev;..."
        providerName="System.Data.SqlClient" />
   </connectionStrings>

, ,

+1

, , , .
- , , ?

, . , , , , , , - , .

0

Source: https://habr.com/ru/post/1742979/


All Articles