Solved

Can an execute query be run within a .NET Dataset?

Posted on 2011-09-20
6
245 Views
Last Modified: 2012-05-12
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
Comment
Question by:11ptMan
  • 3
  • 2
6 Comments
 
LVL 40
ID: 36570730
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
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36570864
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
 

Author Comment

by:11ptMan
ID: 36571322
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:11ptMan
ID: 36571323
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
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 36571506
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
 

Author Closing Comment

by:11ptMan
ID: 36581338
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now