SqlConnection1.ConnectionString

  • Thread starter Thread starter Chen
  • Start date Start date
C

Chen

I have a project which contain several WinForms. I put a SqlDataAdapter in
Many of the Forms.
I want these SqlConnection1.ConnectionString Point to the same SQL Server.
So I create a Global Const strConnection in a Module
and I change the SqlConnection1.ConnectionString lile and set the
SqlConnection1.ConnectionString = strConnection in the code of each form.
(I find I cannot set the SqlConnection1.ConnectionString to strConnection
the property window)
The problem is the VB often automaticely delete the line and complain
ConnectionString not set.
any idea.

Thanks

'Public Const StrConnection As String = "data source=A;..."

'Public Const StrConnection As String = "data source= B;...."

'Public Const StrConnection As String = "data source=C;..."

Public Const StrConnection As String = "data source=P;..."
 
Chen,

I put the connection string in the app.exe.config file so I can easily
change it without having to recompile the application. Then I access it
using the system.configuration access methods (I believe they are
app.settings("name of entry") or appsettings("name of entry")..... Not
sure of the exact name of the method call right now....

The correct form is:

System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")

And in your app.config (or web.config for a web service), add the following
section inside the <configuration></configuration> section:

<appSettings>
<add key="ConnectionString" value="..connection string here.." />
</appSettings>
 
Back
Top