473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to let user select a database connection?

Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow the
user to select the database. We have 2 different databases with the same
schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am very
new to VS2005 so please forgive me for being naïve.

Thanks

Edwin

Oct 26 '07 #1
5 5790
<connectionStrings>
<clear/>
<add name="AdventureWorksString"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;
Initial Catalog=AdventureWorks; Integrated Security=true"/>
</connectionStrings>

individualSettings=configurationManager.Connection Strings[0];

DbConnection MyConnection = null;
switch (individualSettings.ProviderName)
{
case "System.Data.SqlClient":
MyConnection = new SqlConnection(individualSettings.ConnectionString) ;
break;
case "System.Data.OracleClient":
MyConnection = new
OracleConnection(individualSettings.ConnectionStri ng);
break;
case "System.Data.OleDb":
MyConnection = new OleDbConnection(individualSettings.ConnectionStrin g);
break;
case "System.Data.Odbc":
MyConnection = new OdbcConnection(individualSettings.ConnectionString );
break;
}

what u need to do in app.cnfig use connection string and based on provider
name u can change the connection type u also need to initialize coomand and
other object u r using.

now when u change the connection string in app.config it generate the
connection, command object based on coonection string.

"Edwin Smith" wrote:
Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow the
user to select the database. We have 2 different databases with the same
schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am very
new to VS2005 so please forgive me for being naïve.

Thanks

Edwin

Oct 26 '07 #2
Just to add a little extra info to the already good reply:
{
ConnectionStringSettingsCollection connectionStrings =
ConfigurationManager.ConnectionStrings;

ConnectionStringSettings connection;
foreach ( connection in connectionStrings) {

string connectionStringName = connection.Name;
string connectionString = connection.ConnectionString;
string providerName = connection.ProviderName;

Debug.Print(connectionStringName);
}

this.GridView1.DataSource = connectionStrings;
this.GridView1.DataBind();
}

You can bind a grid or anything to these objects above, and get a "picker".
"Som Nath Shukla" <So***********@discussions.microsoft.comwrote in message
news:19**********************************@microsof t.com...
<connectionStrings>
<clear/>
<add name="AdventureWorksString"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;
Initial Catalog=AdventureWorks; Integrated Security=true"/>
</connectionStrings>

individualSettings=configurationManager.Connection Strings[0];

DbConnection MyConnection = null;
switch (individualSettings.ProviderName)
{
case "System.Data.SqlClient":
MyConnection = new
SqlConnection(individualSettings.ConnectionString) ;
break;
case "System.Data.OracleClient":
MyConnection = new
OracleConnection(individualSettings.ConnectionStri ng);
break;
case "System.Data.OleDb":
MyConnection = new
OleDbConnection(individualSettings.ConnectionStrin g);
break;
case "System.Data.Odbc":
MyConnection = new
OdbcConnection(individualSettings.ConnectionString );
break;
}

what u need to do in app.cnfig use connection string and based on provider
name u can change the connection type u also need to initialize coomand
and
other object u r using.

now when u change the connection string in app.config it generate the
connection, command object based on coonection string.

"Edwin Smith" wrote:
>Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow
the
user to select the database. We have 2 different databases with the same
schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am
very
new to VS2005 so please forgive me for being naïve.

Thanks

Edwin


Oct 26 '07 #3
You can make it even easier by using the DbProviderFactory class.

individualSettings=configurationManager.Connection Strings[0];

DbProviderFactory factory =
DbProviderFactories.GetFactory(individualSetting.P roviderName);
DbConnection MyConnection =
factory.CreateConnection(individualSettings.Connec tionString);

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"Som Nath Shukla" <So***********@discussions.microsoft.comwrote in
message news:19**********************************@microsof t.com...
<connectionStrings>
<clear/>
<add name="AdventureWorksString"
providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;
Initial Catalog=AdventureWorks; Integrated Security=true"/>
</connectionStrings>

individualSettings=configurationManager.Connection Strings[0];

DbConnection MyConnection = null;
switch (individualSettings.ProviderName)
{
case "System.Data.SqlClient":
MyConnection = new
SqlConnection(individualSettings.ConnectionString) ;
break;
case "System.Data.OracleClient":
MyConnection = new
OracleConnection(individualSettings.ConnectionStri ng);
break;
case "System.Data.OleDb":
MyConnection = new
OleDbConnection(individualSettings.ConnectionStrin g);
break;
case "System.Data.Odbc":
MyConnection = new
OdbcConnection(individualSettings.ConnectionString );
break;
}

what u need to do in app.cnfig use connection string and based on
provider
name u can change the connection type u also need to initialize coomand
and
other object u r using.

now when u change the connection string in app.config it generate the
connection, command object based on coonection string.

"Edwin Smith" wrote:
>Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow
the
user to select the database. We have 2 different databases with the same
schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am
very
new to VS2005 so please forgive me for being naïve.

Thanks

Edwin

Oct 27 '07 #4
Thanks for the responses everyone. I guess I neglected to mention that my
data set and connections were generated with the VS 2005 designer. I can't
find any of the classes you have mentioned in your responses. To my very
inexperienced brain the designer has obfuscated this beyond belief.

Is there any way to identify the object which contains the information I
need to change?

Thanks

Edwin

"Edwin Smith" <sm**********@aol.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow
the user to select the database. We have 2 different databases with the
same schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am very
new to VS2005 so please forgive me for being naïve.

Thanks

Edwin

Oct 28 '07 #5
OK, I finally I figured out how to change connection strings for datasets
built with the designers.

First, in the Dataset designer window for each table adapter there is a
property called "ConnectionModifier". This must be set to "
public".

Then each table adapters ConnectionString property can be set as follows:

this.myTableAdapter.Connection.ConnectionString = "Database Name =
MyDatabase; Host = MyHost";

Of coursr the Connection String will be whatever your provider needs. The
queries and provider can still be setup with the GUI tools as before then by
use of the above code can be set to whatever connection string you want.

Edwin
"Edwin Smith" <sm**********@aol.comwrote in message
news:uE**************@TK2MSFTNGP02.phx.gbl...
Thanks for the responses everyone. I guess I neglected to mention that my
data set and connections were generated with the VS 2005 designer. I can't
find any of the classes you have mentioned in your responses. To my very
inexperienced brain the designer has obfuscated this beyond belief.

Is there any way to identify the object which contains the information I
need to change?

Thanks

Edwin

"Edwin Smith" <sm**********@aol.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hello:

I'm using VS2005 with an ODBC database.

I am trying to add a Dialog to an existing Database application to allow
the user to select the database. We have 2 different databases with the
same schema so this is a very desirable function.

I can configure 2 different connection strings in app,config but I can't
figure out how to allow the user to select one or the other.

This should be a simple task but I seem to be missing something. I am
very new to VS2005 so please forgive me for being naïve.

Thanks

Edwin


Nov 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ben Binskin | last post by:
Im rather new to developing mysql/php applications and am after some advice on handling user validation for a web based system, ive implimented a number of ways and would like to know which way is...
2
by: hourman | last post by:
We have a web site IIS 5 (on Win2000k) with Oracle 9i backend (Sun unix). There is 1 user ID to oracle(APT_W3) that is used by all users (50). We have a global.asa file that has one APP Start...
8
by: rikesh | last post by:
Hi I'm sure this is a very trivial problem! I have a combo bound to a recordset. I was wondering how I can show the value on the page, what the user has selected? The code that I'm using is...
3
by: teddysnips | last post by:
Currently studying for 70-229. I'm trying to understand how security for users is managed in SQL Server. I've been using SQL Server for a few years now, but without investigating the bits that...
4
by: Wonderinguy | last post by:
Our websphere application uses a generic application userid to connect and query db2 on z/os via DB2 connect. The end user,logs in to the application using his regular userid, which is then...
3
by: KemperR | last post by:
Hello Experts outhere, may be someone can tell me whats going wrong with my ADOX trial. I have an Access 2002 database with some tables and queries (views) The code listed below works well up...
1
by: Tim Dol | last post by:
I have inherited a VB6-SP5 program connecting to an Access 2000 database. I'm relatively new to VB/Access programming, so I may overlook something obvious. I'll try to explain the problem I can't...
1
by: fl | last post by:
I am running ASPNET on my local machine. I have a problem when I try to connect to a SQL server database table. The data looks good when I right click SqlDataAdapter1 to preview the data. When F5...
1
by: Marc Eggenberger | last post by:
Hi there .. I have the following scenario. I have a Webservice which is running under Win2003/IIS6 with .Net1.1 The Service itselfs connects to a database which is a SQL 2000 on a Server in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.