11ptMan
asked on
Can an execute query be run within a .NET Dataset?
Can an execute query be run within a .NET Dataset?
If so can you show me how.
If its not possible how can I accomplish this another way?
I have DataSet which will ultimately have two tables
Singles and Pairs
The Singles Table has the following structure
id (int)
Name (string)
I populate the Singles Table with a service call that returns a list of Single objects
The Couples Table has the following structure
Name1 (string)
Name2 (string)
Compatible (bool)
I want to populate the second table with the results of the following query.
INSERT INTO Couples
SELECT Singles.Name AS Single1, Singles_1.Name AS Single2, 'True' as Compatible
FROM dbo.Singles Singles_1 INNER JOIN
dbo.Singles Singles_1 ON Singles.ID < Singles_1.ID
This is the Code I have so far but have gotten stuck on the last part.
private void button1_Click(object sender, EventArgs e)
{
// Create the container to hold the Singless
List<IdNameEntity> SinglesList = new List<IdNameEntity>();
MainServiceClient MyServiceClient = new MainServiceClient();
MyServiceClient.Open();
SinglesList = MyServiceClient.GetSingles ();
DataTable SinglesTable = new DataTable();
SinglesTable.Columns.Add(n ew DataColumn("Singles1Id", typeof(int)));
SinglesTable.Columns.Add(n ew DataColumn("Singles1", typeof(string)));
foreach(IdNameEntity Singles in SinglesList )
{
DataRow row = SinglesTable.NewRow();
row.SetField("Singles1Id", Singles.Id);
row.SetField("Singles1",Si ngles.Name );
SinglesTable.Rows.Add(row) ;
}
MyDataSet.Tables.Add(Singl esTable);
//Now create a Compatibility Table
DataTable Couples = new DataTable();
Couples.Columns.Add(new DataColumn("Singles1Id", typeof(int)));
Couples.Columns.Add(new DataColumn("Singles1", typeof(string)));
Couples.Columns.Add(new DataColumn("Singles2Id", typeof(int)));
Couples.Columns.Add(new DataColumn("Singles2", typeof(string)));
Couples.Columns.Add(new DataColumn("Compatable", typeof(bool)));
MyDataSet.Tables.Add(Coupl es);
//Write a query to populate the Compatibility Table from the Singles Table
string Comand = "INSERT INTO Couples SELECT SinglesTable.Id AS Single1Id, SinglesTable.Name AS Single1Name, SinglesTable_1.Id AS Single2Id, SinglesTable_1.Name AS Single2Name, 'True' as Compatible FROM dbo.SinglesTable SinglesTable_1 INNER JOIN dbo.SinglesTable SinglesTable_1 ON SinglesTable.ID < SinglesTable_1.ID ";
}
Thank you for any assitance you can provide.
If so can you show me how.
If its not possible how can I accomplish this another way?
I have DataSet which will ultimately have two tables
Singles and Pairs
The Singles Table has the following structure
id (int)
Name (string)
I populate the Singles Table with a service call that returns a list of Single objects
The Couples Table has the following structure
Name1 (string)
Name2 (string)
Compatible (bool)
I want to populate the second table with the results of the following query.
INSERT INTO Couples
SELECT Singles.Name AS Single1, Singles_1.Name AS Single2, 'True' as Compatible
FROM dbo.Singles Singles_1 INNER JOIN
dbo.Singles Singles_1 ON Singles.ID < Singles_1.ID
This is the Code I have so far but have gotten stuck on the last part.
private void button1_Click(object sender, EventArgs e)
{
// Create the container to hold the Singless
List<IdNameEntity> SinglesList = new List<IdNameEntity>();
MainServiceClient MyServiceClient = new MainServiceClient();
MyServiceClient.Open();
SinglesList = MyServiceClient.GetSingles
DataTable SinglesTable = new DataTable();
SinglesTable.Columns.Add(n
SinglesTable.Columns.Add(n
foreach(IdNameEntity Singles in SinglesList )
{
DataRow row = SinglesTable.NewRow();
row.SetField("Singles1Id",
row.SetField("Singles1",Si
SinglesTable.Rows.Add(row)
}
MyDataSet.Tables.Add(Singl
//Now create a Compatibility Table
DataTable Couples = new DataTable();
Couples.Columns.Add(new DataColumn("Singles1Id", typeof(int)));
Couples.Columns.Add(new DataColumn("Singles1", typeof(string)));
Couples.Columns.Add(new DataColumn("Singles2Id", typeof(int)));
Couples.Columns.Add(new DataColumn("Singles2", typeof(string)));
Couples.Columns.Add(new DataColumn("Compatable", typeof(bool)));
MyDataSet.Tables.Add(Coupl
//Write a query to populate the Compatibility Table from the Singles Table
string Comand = "INSERT INTO Couples SELECT SinglesTable.Id AS Single1Id, SinglesTable.Name AS Single1Name, SinglesTable_1.Id AS Single2Id, SinglesTable_1.Name AS Single2Name, 'True' as Compatible FROM dbo.SinglesTable SinglesTable_1 INNER JOIN dbo.SinglesTable SinglesTable_1 ON SinglesTable.ID < SinglesTable_1.ID ";
}
Thank you for any assitance you can provide.
Honestly, I didn't understand the question. Definitely there is a big confusion about how it works.
A DataSet is just a result of a query. Therefore it does not make sense to talk about running a query within a result of query.
I read the answer from JamesBurger and suspect that he didn't understand either. At least, this is my feeling. BTW, can't see big difference between filling a DataSet or a DataTable how JamesBurger proposes. DataSet is basicly a collection of DataTables. So, if you feel a DataSet - you have a Datatable in it.
Anyway, I'd recommend to author to clarify the task.
A DataSet is just a result of a query. Therefore it does not make sense to talk about running a query within a result of query.
I read the answer from JamesBurger and suspect that he didn't understand either. At least, this is my feeling. BTW, can't see big difference between filling a DataSet or a DataTable how JamesBurger proposes. DataSet is basicly a collection of DataTables. So, if you feel a DataSet - you have a Datatable in it.
Anyway, I'd recommend to author to clarify the task.
ASKER
I may need to clarify neither of the tables in the dataset come from a database. I create and populate the first one from a list of objects. I want to populate the second by executing the SQL statement against the first. The SQL statement creates every posible pairing of the items in the first table. I want to save those pairings in the second table. All this happens in memory no DB no connections no filling. The inital list of objects comes from a service call that I have no ability to alter. Hope this helps to clarify my question. Thank You
ASKER
I may need to clarify neither of the tables in the dataset come from a database. I create and populate the first one from a list of objects. I want to populate the second by executing the SQL statement against the first. The SQL statement creates every posible pairing of the items in the first table. I want to save those pairings in the second table. All this happens in memory no DB no connections no filling. The inital list of objects comes from a service call that I have no ability to alter. Hope this helps to clarify my question. Thank You
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
While No was not really the answer I was looking for it was the one I kind of expected. LINQ will do what i needed without needing any DataSets.
Here is the Updated version of the code
private void button6_Click(object sender, EventArgs e)
{
// Create the container to hold the Singles
IQueryable<IdNameEntity> SinglesList;
MainServiceClient MyServiceClient = new MainServiceClient();
MyServiceClient.Open();
SinglesList = MyServiceClient.GetSingles ().AsQuery able();
// Create the container to hold the Couples
IQueryable<string> Couples;
Couples = from a in SinglesList
from b in SinglesList
where a.Id.CompareTo(b.Id) < 0
select string.Format("{0}:{1}", a.Id.ToString(), b.Id.ToString());
}
Here is the Updated version of the code
private void button6_Click(object sender, EventArgs e)
{
// Create the container to hold the Singles
IQueryable<IdNameEntity> SinglesList;
MainServiceClient MyServiceClient = new MainServiceClient();
MyServiceClient.Open();
SinglesList = MyServiceClient.GetSingles
// Create the container to hold the Couples
IQueryable<string> Couples;
Couples = from a in SinglesList
from b in SinglesList
where a.Id.CompareTo(b.Id) < 0
select string.Format("{0}:{1}", a.Id.ToString(), b.Id.ToString());
}
Another alternative is a DataReader (there are good samples of its use in online help). You use a DataTable to loop through the lines in the resulting query the same way you do when loop through through a file in sequential access. Read one line, do what you want to do with it, then read the following line, until there is no more line left.
The do what you want to do with it is up to you. You can fill a DataTable, a ComboBox, a DataGridView, a collection.
If all you want to do is get only one value from the database, or execute a command that does not return values, such as INSERT, UPDATE and DELETE, you can also do it directly through the Command object, by executing one of its numerous methods, the most commonly used being ExecuteScalar (to retrieve a unique value) and ExecuteNonQuery (to execute a command that does not return a value). There are good examples of all of those too in online help.