Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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(new DataColumn("Singles1Id", typeof(int)));
            SinglesTable.Columns.Add(new DataColumn("Singles1", typeof(string)));
           
            foreach(IdNameEntity Singles in SinglesList )
            {
                DataRow row = SinglesTable.NewRow();
                row.SetField("Singles1Id", Singles.Id);
                row.SetField("Singles1",Singles.Name);
                SinglesTable.Rows.Add(row);
            }

            MyDataSet.Tables.Add(SinglesTable);

            //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(Couples);

            //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.
0
11ptMan
Asked:
11ptMan
  • 3
  • 2
1 Solution
 
Jacques Bourgeois (James Burger)PresidentCommented:
You can fill a DataTable instead of a DataSet, which is recommended anyway if you have only one table or if you have many tables that are not linked through a DataRelation. Use the Fill command, the same way you do with a DataSet, but replace this one by your DataTable

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.
0
 
anarki_jimbelCommented:
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.
0
 
11ptManAuthor Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
11ptManAuthor Commented:
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
0
 
anarki_jimbelCommented:
No, in DataTables in memory you cannot do that. It is possible to select rows but it is not possible to run SQL queries.

Alternatives:

1. Use LINQ. Honestly, not 100% sure it is achievable with LINQ.

2. Use traditional loops etc.
0
 
11ptManAuthor Commented:
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().AsQueryable();

            // 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());

        }
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now