473,473 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing a DataAdapter connection

Hello group,

I have several servers hosting SQL databases. On each of them, I have
several databases. All those databases have the same structure (even those
on different servers), only the data changes.
I made a winforms application that allows me to manage those databases
easily, and used several SqlDataAdapter to achieve this, all of them filling
the same Dataset which has relations, and this Dataset being the DataSource
of a DataGrid. I can then play with the data and hit my "Save" button to do
an Update on all the DataAdapters.
I have a ComboBox that let me chose the server, and another one for the
database (user ID and password are the same everywhere).

I first made my DataAdapters in design mode, because I can just ask for a
"SELECT *" and all the long code for inserting/updating/deleting will be
written for me. At this step, I had to provide an SqlConnection and chose
one of the database on one of the servers. All works fine.

Now I'd like to take my two comboboxes into account, and change the
connection used by the DataAdpaters. Is this possible ?
Here is what is called when either combobox changes (connection is a private
member of the Form):

public void cbb_SelectedIndexChanged(object sender, System.EventArgs e)

{

if (cbbServer.SelectedIndex > -1 && cbbDatabase.SelectedIndex > -1)

connection = new SqlConnection("User ID='sa';Password='';Server='" +
cbbServer.Items[cbbServer.SelectedIndex].ToString() +

"';Database='" + cbbDatabase.Items[cbbDatabase.SelectedIndex].ToString() +
"'");

dataset = new DataSet();

dataset.Tables.Add("T1");

dataset.Tables.Add("T2");

dataset.Tables.Add("T3");

dataset.Tables.Add("T4");

connection.Open();

adapter1.Fill(dataset, "T1");

adapter2.Fill(dataset, "T2");

adapter3.Fill(dataset, "T3");

adapter4.Fill(dataset, "T4");

connection.Close();

dataset.Relations.Add("R1-2", dataset.Tables["T1"].Columns["T1_ID"],
dataset.Tables["T2"].Columns["T2_ID_T1"]);

dataset.Relations.Add("R2-3", dataset.Tables["T2"].Columns["T2_ID"],
dataset.Tables["T3"].Columns["T3_ID_T2"]);

dataset.Relations.Add("R3-4", dataset.Tables["T3"].Columns["T3_ID"],
dataset.Tables["T4"].Columns["T4_ID_T3"]);

dgModeles.DataSource = dataset.Tables[0];

}

Karine Proot

G-Fit
Jul 21 '05 #1
3 3893
Hi,

Yes, it is possible.
Assign new connection to adapter's command instances (SelectCommand, ...).

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"G-Fit" <ms**@g-fit.fr> wrote in message
news:uj*************@TK2MSFTNGP11.phx.gbl...
Hello group,

I have several servers hosting SQL databases. On each of them, I have
several databases. All those databases have the same structure (even those
on different servers), only the data changes.
I made a winforms application that allows me to manage those databases
easily, and used several SqlDataAdapter to achieve this, all of them filling the same Dataset which has relations, and this Dataset being the DataSource of a DataGrid. I can then play with the data and hit my "Save" button to do an Update on all the DataAdapters.
I have a ComboBox that let me chose the server, and another one for the
database (user ID and password are the same everywhere).

I first made my DataAdapters in design mode, because I can just ask for a
"SELECT *" and all the long code for inserting/updating/deleting will be
written for me. At this step, I had to provide an SqlConnection and chose
one of the database on one of the servers. All works fine.

Now I'd like to take my two comboboxes into account, and change the
connection used by the DataAdpaters. Is this possible ?
Here is what is called when either combobox changes (connection is a private member of the Form):

public void cbb_SelectedIndexChanged(object sender, System.EventArgs e)

{

if (cbbServer.SelectedIndex > -1 && cbbDatabase.SelectedIndex > -1)

connection = new SqlConnection("User ID='sa';Password='';Server='" +
cbbServer.Items[cbbServer.SelectedIndex].ToString() +

"';Database='" + cbbDatabase.Items[cbbDatabase.SelectedIndex].ToString() +
"'");

dataset = new DataSet();

dataset.Tables.Add("T1");

dataset.Tables.Add("T2");

dataset.Tables.Add("T3");

dataset.Tables.Add("T4");

connection.Open();

adapter1.Fill(dataset, "T1");

adapter2.Fill(dataset, "T2");

adapter3.Fill(dataset, "T3");

adapter4.Fill(dataset, "T4");

connection.Close();

dataset.Relations.Add("R1-2", dataset.Tables["T1"].Columns["T1_ID"],
dataset.Tables["T2"].Columns["T2_ID_T1"]);

dataset.Relations.Add("R2-3", dataset.Tables["T2"].Columns["T2_ID"],
dataset.Tables["T3"].Columns["T3_ID_T2"]);

dataset.Relations.Add("R3-4", dataset.Tables["T3"].Columns["T3_ID"],
dataset.Tables["T4"].Columns["T4_ID_T3"]);

dgModeles.DataSource = dataset.Tables[0];

}

Karine Proot

G-Fit

Jul 21 '05 #2
Hi Karine,

Why not have a look at this page

http://msdn.microsoft.com/library/de...ctortopic3.asp

If it is not clear ask again?

Cor
Jul 21 '05 #3
"Miha Markic [MVP C#]" <miha at rthand com> a écrit dans le message de
news:OY**************@TK2MSFTNGP11.phx.gbl...
Hi,

Yes, it is possible.
Assign new connection to adapter's command instances (SelectCommand, ...).

"Cor Ligthert" <no**********@planet.nl> a écrit dans le message de
news:eZ****************@TK2MSFTNGP09.phx.gbl... Hi Karine,

Why not have a look at this page

http://msdn.microsoft.com/library/de...ctortopic3.asp
Ah, thanks. I was looking for a property like DataAdapter.Connection, and
didn't see it was its commands which had it. My bad.
Works great now !

Solution below :
Just added in my "if" block :

if (cbbServer.SelectedIndex > -1 && cbbDatabase.SelectedIndex > -1)

{

connection = new SqlConnection("User ID='sa';Password='';Server='" +
cbbServer.Items[cbbServer.SelectedIndex].ToString() + "';Database='" +
cbbDatabase.Items[cbbDatabase.SelectedIndex].ToString() + "'");

adapter1.SelectCommand.Connection = connection;

adapter1.InsertCommand.Connection = connection;

adapter1.UpdateCommand.Connection = connection;

adapter1.DeleteCommand.Connection = connection;

adapter2.SelectCommand.Connection = connection;

adapter2.InsertCommand.Connection = connection;

adapter2.UpdateCommand.Connection = connection;

adapter2.DeleteCommand.Connection = connection;

adapter3.SelectCommand.Connection = connection;

adapter3.InsertCommand.Connection = connection;

adapter3.UpdateCommand.Connection = connection;

adapter3.DeleteCommand.Connection = connection;

adapter4.SelectCommand.Connection = connection;

adapter4.InsertCommand.Connection = connection;

adapter4.UpdateCommand.Connection = connection;

adapter4.DeleteCommand.Connection = connection;

}
Jul 21 '05 #4

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

Similar topics

20
by: TJ Doherty | last post by:
Need help understanding the following please: When I am creating a project and code my connection using Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data...
3
by: Stephen Noronha | last post by:
I have a question, please correct me if I am wrong. I am assuming that a dataadapter establishes a connection and after filling the dataset or datatable or whatever, will close the connection to...
13
by: Doug Bell | last post by:
Hi, I thought I had this sorted this morning but it is still a problem. My application has a DataAccess Class. When it starts, it: Connects to a DB (OLE DB) If it connects it uses an...
3
by: G-Fit | last post by:
Hello group, I have several servers hosting SQL databases. On each of them, I have several databases. All those databases have the same structure (even those on different servers), only the data...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
3
by: Bob | last post by:
Over the life of a distributed app, it is possible for the connection string that it was configured with initailly needs to change. You can't scope a connection string setting to user so that it...
5
by: Emil | last post by:
I've created a very simple data base in Microsoft Access 2003. It consists of only one table called "Students" and it contains 2 information about each student: id-student and name. When I try...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.