Re: Changing Connection String programmatically



This is a good situation for putting the connection string information in the app.config file. You will not have to recompile and redeploy the application each year, only the app.config file.

Add an Application.config file to the solution, and then add the appSettings section. You can add as many 'keys' as you wish. They are just name=value pairs that the application can retrieve.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="DATA Source=ServerName;initial catalog=MyDatabase;integrated security=True" />
</appSettings>
</configuration>

Then in the application use:

for VB: Dim con As SqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
for C#: SqlConnection con = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))

You may wish to check further in VS Help about using an app.config file.

--
Arnie Rowland
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


"Zsolt" <Zsolt@xxxxxxxxxxxxxx> wrote in message news:DD87AECE-A6FF-4C8B-ABA6-B5B80B89D9A6@xxxxxxxxxxxxxxxx
Thank you for your answer, Arnie!

The reason for using different databases of the same design is that
every year we will create new database. After creating the new years
database, we will register it in the master database, entering the
necessary connection data in a table. DataSet should use any years
database, but only one at a time.

My problem is that how can I pass it to the DataSet, because it is
private. Of course I can edit the MyDataSet.Designer.cs file,
change the body of the private void InitConnection(), and it works:

private void InitConnection() {
this._connection = new System.Data.SqlClient.SqlConnection();
// insted of:
// this._connection.ConnectionString = "Data Source=SQL\\SQLEXPRESS;Initial
Catalog=Data2006;Integrated Security=True";
// This:
this._connection.ConnectionString = MyConnStrinProvider.GetConnString();
}

But whenever I make some changes on .xsd, the changes above are lost.
Changing of an <auto-generated> file seems not good.

I think there must be better solution then this.

Thank you again,
Zsolt


"Arnie Rowland" wrote:

Since if the SQL Server holding your connection strings is renamed or
changed, and you will not be able to get to the connection strings, that
seems like a bad idea.

Why not save the connection string(s) in the application.config file?

If you are storing connnection string information related to additional
servers, and you will use a confirmed static connection to retreive the
stored strings, just put them in a table. You may wish to have different
fields for server, database, security context, etc.. Create a stored
procedure that will provide you the concatenated string when you pass it a
parameter.

--
Arnie Rowland
"To be successful, your heart must accompany your knowledge."



"Zsolt" <Zsolt@xxxxxxxxxxxxxx> wrote in message
news:1173DEA0-9696-4C12-8F58-ACA1926D85D8@xxxxxxxxxxxxxxxx
How can I change the Connection String programmatically?
(WS2005, Windows forms, C#)

I need to change the server name, or Initial Catalog parts of the string.
My connection strings are in a field of a master SQL database table.
The design of the different databases are the same.
How can I pass it to the _connection in the dataset?
_connection and InitConnection() are private.
I know, that I can edit the MyDbDataSet.Designer.cs, but I don't
belive, that it is good solution.

private void InitConnection() {
this._connection = new System.Data.SqlClient.SqlConnection();
this._connection.ConnectionString = "Data
Source=SQL\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated
Security=" +
"True";
}

If the Data Source Confoguration Wizard saves the connection string,
the Settings.Designer.cs has also private properties I don't want edit.

Thanks - Zsolt





Relevant Pages

  • Complicated Connection Problems bewteen ADP and SQL Server
    ... This database ... expertise for getting the user workstations talking to the SQL Server. ... connection would fail and the adp wouldn't be able to talk to the server. ... might be in my ADO connection string. ...
    (microsoft.public.access.adp.sqlserver)
  • Complicated Connection Problem between ADP and SQL Server
    ... This database ... expertise for getting the user workstations talking to the SQL Server. ... connection would fail and the adp wouldn't be able to talk to the server. ... might be in my ADO connection string. ...
    (microsoft.public.sqlserver.connect)
  • Re: Problems in loading data from an access database into an array
    ... Suppose your database has a table called which has columns called ... Dim oleCSB As New OleDbConnectionStringBuilder ... 'associate the connection string just built with the ole connection ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Syntax
    ... The connection string format is not valid. ... connection string with zero components is set on database connection manager. ... Access database connection string, and not the Expression issue you're ...
    (microsoft.public.sqlserver.dts)
  • Re: Complicated Connection Problems bewteen ADP and SQL Server
    ... front end on the 5-8 workstations where the users happen to sit. ... expertise for getting the user workstations talking to the SQL Server. ... connection would fail and the adp wouldn't be able to talk to the server. ... thought that I should instead change my connection string to something ...
    (microsoft.public.access.adp.sqlserver)