473,385 Members | 1,351 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,385 software developers and data experts.

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 3883
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: 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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...

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.