I wish to create an array of DataTables with the same columns. Is there a way to set the schema of all the DataTables in one shot instead of running through each DataTable and add the columns?
No, you can't do this without a loop. Even if there was a method like DataSet.CloneManyTables it would use a loop for you. A LINQ solution would also use a loop. So use following:
You can use DataTable.Clone, for example with 100 clone tables:
for (int i = 1; i <= 100; i++)
{
DataTable tClone = tSource.Clone(); // tSource is your source-table
tClone.TableName = $"{tClone.TableName}_{i + 1}";
ds.Tables.Add(tClone); // ds is your DataSet
}
The columns can have the same names but the table-name must be unique.
Related
I have a dataset that looks like the image. I'm trying to filter by table and get all the columns next to it and compare them against other datasets
This dataset has tables named table 1 and table 2 and when they're selected they look like the picture below. It shows the columns and I need to compare those columns against the rows from the matching table in the first dataset
I've looked at dataview but that would be a lot of work and I'm very inexperienced. I'm trying to find a way to implement a foreach loop that'll get the name of the table in the first dataset and then compare the rows in it against the columns inside the datatable in the second dataset that matched the table name from the first dataset.
Without knowing more about these DataSets (like do they have primary keys, the data types of the columns, the number of rows in each table, etc), I can only provide limited help. The following example tries to be as general as possible and avoid some basic problems:
DataSet ds1 = <<fetch dataset1>>;
DataSet ds2 = <<fetch dataset2>>;
foreach (DataTable tbl1 in ds1.Tables)
{
if (ds2.Tables.Contains(tbl1.TableName))
{
DataTable tbl2 = ds2.Tables[tbl1.TableName];
List<string> commonColumnNames = new List<string>(tbl1.Columns.Cast<DataColumn>().Select(c => c.ColumnName).Intersect(tbl2.Columns.Cast<DataColumn>().Select(c => c.ColumnName)));
int maxRows = Math.Min(tbl1.Rows.Count, tbl2.Rows.Count);
for (int r = 0; r <= maxRows; r++)
{
foreach (string colName in commonColumnNames)
{
if (tbl1.Rows[r][colName] != tbl2.Rows[r][colName])
{
// Different value
}
}
}
}
}
Update 1: I've added comments to the following example to explain step-by-step what this code is doing. As I try to say before, since I didn't know much about your data, I had to put in extra code. This extra code is for things like: 'Does the table ABC exist in both DataSets?', 'Do the two tables have the same columns in them?', 'Do the tables have the same number of rows in them?'. Your original question did not have this information, so I made this code a little more robust to handle those unknowns.
DataSet ds1 = <<fetch dataset1>>;
DataSet ds2 = <<fetch dataset2>>;
// Loop through all of the tables in the 1st DataSet
foreach (DataTable tbl1 in ds1.Tables)
{
// If the 2nd DataSet has a table with same name as the one from the 1st DataSet
if (ds2.Tables.Contains(tbl1.TableName))
{
DataTable tbl2 = ds2.Tables[tbl1.TableName];
// Create a list of column names that the two tables have in common.
// We will only compare the values in these two tables, in this set of matching column names.
List<string> commonColumnNames = new List<string>(tbl1.Columns.Cast<DataColumn>().Select(c => c.ColumnName).Intersect(tbl2.Columns.Cast<DataColumn>().Select(c => c.ColumnName)));
// Before we start comparing the rows in the two tables, find out which one has the fewer number of rows in it.
int maxRows = Math.Min(tbl1.Rows.Count, tbl2.Rows.Count);
// If the tables have a different number of rows, then we will only compare the set of rows numbered 0-to-MinRowCount
for (int r = 0; r <= maxRows; r++)
{
// For each row, compare the values of common columns
foreach (string colName in commonColumnNames)
{
if (tbl1.Rows[r][colName] != tbl2.Rows[r][colName])
{
// Different value
}
}
}
}
}
I am having trouble updating the database from a dataset. The data is inserted into the datatable in the dataset correctly and I can see it in a datagridview, but when I look at the database the columns have not been added
This is my code below:
TimeSpan timeDif = dateToSet.Subtract(dateInTable);
int timeDifference = (int)timeDif.Days;
for (int i = 0; i < timeDifference; i++)
{
DataColumn columnToAdd = new DataColumn(dateInTable.AddDays(i + 1).ToShortDateString());
Program.pDiResourcesDataSet.WorkingProjects.Columns.Add(columnToAdd);
Program.WorkingProjectsA.Update(Program.pDiResourcesDataSet.WorkingProjects);
}
Program.pDiResourcesDataSet.AcceptChanges();
Program.WorkingProjectsA.Update(Program.pDiResourcesDataSet);
As rene said, you cannot add columns with dataadapter, rather use separate DDL statement to carry out the DDL operations, and use dataadapter to manipulate the records
Is it possible to add a row to the middle of an existing dataset with c#? I've done a lot of searching and haven't been able to find anything on how to do this. What have I tried? I've tried searching a lot and haven't found anything like an 'insertAt' method for datasets.
Thanks
Mike
The DataSet consists of a collection of DataTable objects so I assume that you are talking about a Datatable, right? If so, it has an InsertAt method:
DataTable dt = dataset.Tables[0]; //Get first datatable from dataset
DataRow row = dt.NewRow();
//fill row
dt.Rows.InsertAt(row,3); //Insert at index 3
DataSet does not have a rows collection, so you can't add a row to it at all.
You can insert a row by index into a DataTable object using DataTable.Rows.InsertAt(row, i). If the table is in a DataSet, your syntax would be DataSet.Tables[i].Rows.InsertAt(row, 0)
In my opinion (though this could take a lot of time), you can create an array or a list array then transfer all the data there from your dataset through for loop or any loop...then put an if statement inside to check where you want to put your extra data like this:
List<string> arrayList = dataset;// i know this is not possible just showing you that you have to put all your data from dataset to array:)
List <string> newList = new List<string>();//its up to you if you want to put another temporary array or you could simply output your data from the loop.
//THE LOOP
for(int i = 0; i<=arrayList.Count(); i++){
if(i == x)//x is the index or you may change this statement its up to you
{
//do the print or pass the data to newList
newList.add(arraList[i]);//not sure about this. its been a while since the last time i use this array list..
}
}
another way is customize your query(if your pulling out some data from database)
happy coding:)
Here's a short sample of doing it:
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Table");
dt.Columns.Add("Id");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(new object[]{i});
}
var newRow=dt.NewRow();
newRow.ItemArray=new string[]{(dt.Rows.Count/2).ToString()+".middle"};
dt.Rows.InsertAt(newRow, dt.Rows.Count / 2);
}
}
I have one asp.net datatable and I want to databind into two asp.net datalists, so I though to slice the datatable rows in two datatables both the same size if even .
Use the Take LINQ extension method to specify how many items to use.
And the Skip to jump over if needed.
var half = myList.Take(myList.Count / 2);
If you are slicing by rows, you can simply create a copy of your original data table, find a suitable half way point, and just import the rows into the copy, while deleting them from the original.
Something like the following should work:
DataTable originalTable = new DataTable();
//Load the data into your original table or wherever you get your original table from
DataTable otherTable = originalTable.Copy(); //Copys the table structure only - no data
int rowCount = originalTable.Rows.Count;
int wayPoint = rowCount / 2; //NB integer division rounds down towards 0
for(int i = 0; i <= wayPoint; i++)
{
otherTable.ImportRow(originalTable.Rows[i]); //Imports (copies) the row from the original table to the new one
originalTable.Rows[i].Delete(); //Marks row for deletion
}
originalTable.AcceptChanges(); //Removes the rows we marked for deletion
I'm trying to add to a new DataSet X a DataTable that is inside of a different DataSet Y.
If I add it directly, I get the following error:
DataTable already belongs to another DataSet.
Do I have to clone the DataTable and import all the rows to it and then add the new DataTable to the new DataSet? Is there a better/easy way to do it?
There are two easy ways to do this:
DataTable.Copy
Instead of DataTable.Clone, use DataTable.Copy to create a copy of your data table; then insert the copy into the target DataSet:
dataSetX.Tables.Add( dataTableFromDataSetY.Copy() );
DataSet.Merge
You could also use DataSet.Merge for this:
dataSetX.Merge(dataTableFromDataSetY);
Note, however, that if you are going to use this method, you might want to make sure that your target DataSet doesn't already contain a table with the same name:
If the target DataSet doesn't contain a table by the same name, a fresh copy of the table is created inside the data set;
If a table by the same name is already in the target data set, then it will get merged with the one passed to Merge, and you end up with a mix of the two.
Use this generic function
public DataTable CopyDataTable(DataTable dtSource, int iRowsNeeded)
{
if (dtSource.Rows.Count > iRowsNeeded)
{
// cloned to get the structure of source
DataTable dtDestination = dtSource.Clone();
for (int i = 0; i < iRowsNeeded; i++)
{
dtDestination.ImportRow(dtSource.Rows[i]);
}
return dtDestination;
}
else
return dtSource;
}
Suppose you want to copy first table of sourceSet to destinationSet.
Call it like
DataTable destinationTable = CopyDataTable(sourceSet.Tables[0], sourceSet.Tables[0].Rows.Count);
DataSet destinationSet = new DataSet();
destinationSet.Tables.Add(destinationTable);
This should work for you
X.Tables.Add(Y.Tables[<tablename|index>].Copy());
IEnumerable<DataRow> query = from sourceTbl in sourceTbl.AsEnumerable()
where [any condition you want]
select order;
DataTable boundTable = query.CopyToDataTable<DataRow>();
I am not sure If i understood your question properly.Hope this code helps