I have a SQL Server table called tSongList that contains the following information:
colAlbumID, colSongName, colAlbumTrackNumber, colRequestedCount, colPlayPriority
The purpose of this table is to help a DJ keep a list of which songs the DJ should play and which album they are from. I have a C# class that will take a list of songs from a specific albumID and calculate the colPlayPriority based on the colRequestedCount. I have designed this class to take a DataTable containing the columns above and compute the necessary information.
So my question is if I want to use SQL to select all the rows from the tSongList, how do I get the SQL result into multiple DataTables grouped by colAlbumID? In other words, I want a DataTable for each Album that contains it's song information.
I know that I can use a SqlDataAdapter to fill a DataSet and since a DataSet can contain multiple DataTables, is there a way to construct a SQL query to return a DataSet containing the DataTables grouped by albumID?
Also if this can't be done, should I just select everything into one DataTable and use the Select function to get a DataRow array instead?
One option is to return a single DataTable and use LINQ to create your grouping.
var albumGroups = from a in dtAlbums.AsEnumerable()
group a by a.Field<int>("albumID") into g
select new { colSongName = g.Field<string>("colSongName"),
colAlbumTrackNumber = g.Field<int>("colAlbumTrackNumber"),
colRequestedCount = g.Field<int>("colRequestedCount"),
colPlayPriority = g.Field<int>("colPlayPriority") };
I think the example on MSDN will show you how to do this. You will make life much easier for yourself if you properly normalise your data first. You can create a hierarchical DataSet with the necessary relationships in place making it trivial to get all songs for an album.
Related
At the moment, I source my data from a SQL serve r(2008) database. The cyurrent method is to use a DataTable, which is then passed around and used.
if (parameters != null)
{
SqlDataAdapter _dataAdapter = new SqlDataAdapter(SqlQuery, CreateFORSConnection());
foreach (var param in parameters)
{
_dataAdapter.SelectCommand.Parameters.AddWithValue(param.Name, param.Value);
}
DataTable ExtractedData = new DataTable(TableName);
_dataAdapter.Fill(ExtractedData);
return ExtractedData;
}
return null;
But now, the user has said that we can also get data from txt files, which have the same structure as the tables in SQL Server. So, if I have a table called 'Customer', then I have a csv file with Customer. with the same column structure. The first line in the CSV is the column name, and matches my tables.
Would it be possible to read the txt file into a data table, and then run a SELECT on that data table somehow? Most of my queries are single table queries:
SELECT * FROM Table WHERE Code = 111
There is, however, ONE case where I do a join. That may be a bit more tricky, but I can make a plan. If I can get the txt files into data tables first, I can work with that.
Using the above code, can I not change the connection string to rather read from a CSV instead of SQL Server?
First, you'll need to read the CSV data into a DataTable. There are many CSV parsers out there, but since you prefer using ADO.NET, you can use the OleDB client. See the following article.
http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-built-in-oledb-csv-parser
Joining is a bit harder, since both sets of data live in different places. But what you can do is get two DataTables (one from each source), then use Linq to join them.
Inner join of DataTables in C#
You could read the text file into a List<string> (if there is just 1 column per file), and then use LINQ to query the list. For example:
var result = from entry in myList
where entry == "111"
select entry;
Of course, this example is kind of useless since all you get back is the same string you are searching for. But if there are multiple columns in the file, and they match the columns in your DataTable, why not read the file into the data table, and then use LINQ to query the table?
Here is a simple tutorial about how to use LINQ to query a DataTable:
http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx
I'm having a bit of difficulty with datasets in C#. I know how to load datasets and everything, and I can drag a table from the dataset into the form window and it displays the columns etc of that table. However, I would like to display every single table in the form, but in a clean way. Is there a way to create a dropdownlist for example, that will display all the tables in the data set, and then I can select the one I want and then display the columns, etc? Just need a way to display tables in the form and navigate between them and have them display their data. How would I go about doing this?
First of all you need to create an instance of DataSet (typed dataset) (say DatabaseDataSet)
DatabaseDataSet ds = new DatabaseDataSet();
Then create adapter's instance which is an autogenerated class.
DatabaseDataSetTableAdapters.yourTableTableAdapter adp;
adp=new DatabaseDataSetTableAdapters.yourTableTableAdapter();
Pupulate the datatable object and push into to ds.
adp.Fill(ds.yourTable);
Use DataBinding technique to show data from the dataset object.
comboBox1.DataSource = ds.Tables["yourTable"];
comboBox1.DisplayMember = "column1";
comboBox1.ValueMember = "column2";
Off-topic:Use Collections, LINQ and Entity framework. The DataSet is bit outdated and it has many problems. Please take a look at this MSDN post.
DataSets vs. Collections
I have a list of companies in DataTable format which is static and accessible across my application.
Different Forms in the application just show filtered Data like Customer Companies ,Supplier Companies ,Misc etc in a DataGridView.
The DataGridViews are just for the sake of displaying the Data.
If I query my companies DataTable for only customer companies and assign the result to DatagridView.Datasource, it will just make a copy and assign it DataGridView.
Imagine if I have a list of 10,000 Customers and User open 10 forms for different purpose, I will be just duplicating 10,000 * 10 rows of data.
Also if my main Companies DataTable is Updated , I will have to update all the DataGridViews too.
Will using DataViews help me save the memory ?
Will BindingDatasource do good?
or is there any better method to share DataTable without making copies in memory for just Displaying Purpose.
Regards
Given:
I have a list of companies in
DataTable format which is static and
accessible across my application
I assume 1 DataTable. You should use RowFilter see MSDN: RowFilter:
DataGridView customerView;
DataGridView supplierView; // initialize in form
DataTable companiesTable; // initialized and filled
void SetCustomerCompanyView()
{
DataView cust = new DataView();
cust.Table = companiesTable;
cust.RowFilter = "Type = 'Customer'";
customerView.DataSource = cust;
}
// repeat for SetSupplierCompanyView()
If the concern is that one large DataTable is going to be copied throughout multiple active views, then you should really re-think the architecture. If a form is hidden or inactive, you can always dispose of the query results (in worst case) and re-query if/when the form becomes visible or active again (such as a tabbed form). For what it's worth, 10k records is easily handled in most systems and should accommodate several open views (filtered as above) of that DataTable.
An overtly simplistic comparison:
You can think of a DataView as a List<int> where int would be a row index. When you apply a row filter, the appropriate row indexes are appended to the List. You could easily create a view using LINQ to DataSets - extracting the row indexes you require based on a column or columns of data and append to the List. Now you use that list to reference only the rows (by Table.Rows[RowIndex]) in which you are interested from your primary DataTable.As stated, this is much simpler functionality than what the DataView is actually providing. You have not made a copy of the DataTable - you have simply created a shallow reference mechanism.
I have a DataSet which contains two tables, Publication and Owner, which are linked on Publication ID. How do I query the dataset? What I am trying to do is get all of the owners for a particular publication, and then I want to iterate over the resulting set, concatenate the owner names together and populate a label with the information...
But lets begin with, how do i query the dataset?
I also have a DataRelation, can I query that somehow to get the child rows for the current row?
Thanks.
ADO.NET supports two fundamental approaches for performing filtering and sorting of datasets:
The DataTable Select Method - This method is overloaded to accept arguments to filter and sort data rows returning an array of DataRow objects.
The DataView object sort, filter and find methods - This object uses the same filter arguments supported by the Select method, but the DataView exposes structures that can be bound to data-aware controls. See DataView.RowFilter
Iterating over filtered rows is as easy as:
DataTable dt;
...
foreach (DataRow dr in dt.Select(filter))
{
// ...
}
This article contains several examples: A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 1
You could look into LINQ to Dataset, which allows you to perform queries against DataSets with multiple tables. You can perform joins between the tables on the appropriate columns amongst other things.
I am writing this in C# using .NET 3.5. I have a System.Data.DataSet object with a single DataTable that uses the following schema:
Id : uint
AddressA: string
AddressB: string
Bytes : uint
When I run my application, let's say the DataTable gets filled with the following:
1 192.168.0.1 192.168.0.10 300
2 192.168.0.1 192.168.0.20 400
3 192.168.0.1 192.168.0.30 300
4 10.152.0.13 167.10.2.187 80
I'd like to be able to query this DataTable where AddressA is unique and the Bytes column is summed together (I'm not sure I'm saying that correctly). In essence, I'd like to get the following result:
1 192.168.0.1 1000
2 10.152.0.13 80
I ultimately want this result in a DataTable that can be bound to a DataGrid, and I need to update/regenerate this result every 5 seconds or so.
How do I do this? DataTable.Select() method? If so, what does the query look like? Is there an alternate/better way to achieve my goal?
EDIT: I do not have a database. I'm simply using an in-memory DataSet to store the data, so a pure SQL solution won't work here. I'm trying to figure out how to do it within the DataSet itself.
For readability (and because I love it) I would try to use LINQ:
var aggregatedAddresses = from DataRow row in dt.Rows
group row by row["AddressA"] into g
select new {
Address = g.Key,
Byte = g.Sum(row => (uint)row["Bytes"])
};
int i = 1;
foreach(var row in aggregatedAddresses)
{
result.Rows.Add(i++, row.Address, row.Byte);
}
If a performace issue is discovered with the LINQ solution I would go with a manual solution summing up the rows in a loop over the original table and inserting them into the result table.
You can also bind the aggregatedAddresses directly to the grid instead of putting it into a DataTable.
most efficient solution would be to do the sum in SQL directly
select AddressA, SUM(bytes) from ... group by AddressA
I agree with Steven as well that doing this on the server side is the best option. If you are using .NET 3.5 though, you don't have to go through what Rune suggests. Rather, use the extension methods for datasets to help query and sum the values.
Then, you can map it easily to an anonymous type which you can set as the data source for your grid (assuming you don't allow edits to this, which I don't see how you can, since you are aggregating the data).
I agree with Steven that the best way to do this is to do it in the database. But if that isn't an option you can try the following:
Make a new datatable and add the columns you need manually using DataTable.Columns.Add(name, datatype)
Step through the first datatables Rows collection and for each row create a new row in your new datatable using DataTable.NewRow()
Copy the values of the columns found in the first table into the new row
Find the matching row in the other data table using Select() and copy out the final value into the new data row
Add the row to your new data table using DataTable.Rows.Add(newRow)
This will give you a new data table containing the combined data from the two tables. It won't be very fast, but unless you have huge amounts of data it will probably be fast enough. But try to avoid doing a LIKE-query in the Select, for that one is slow.
One possible optimization would be possible if both tables contains rows with identical primary keys. You could then sort both tables and step through them fetching both data rows using their array index. This would rid you of the Select call.