Return Multiple DataTable in C# 2005 - c#

I have table1 and table2 in a class..
public DataTable sampletable (DataTable table1,DataTable table2)
{
// How to return the two table(table1 and table2)
}
Advance thank you

public DataTable[] sampletable (DataTable table1,DataTable table2)
{
return new DataTable[] { table1, table2 };
}
Use an array. And to retrieve a particular table:
DataTable[] dtArray = sampletable (YourFirstDt, YourSecondDt);
DataTable table1 = dtArray[0];
DataTable table2 = dtArray[1];

Assuming they have the same schema, you can use the DataTable.Merge Method
public DataTable sampletable(DataTable table1, DataTable table2)
{
table1.Merge(table2);
return table1;
}
The Merge method is used to merge two DataTable objects that have
largely similar schemas. A merge is typically used on a client
application to incorporate the latest changes from a data source into
an existing DataTable. This allows the client application to have a
refreshed DataTable with the latest data from the data source.
The merge operation takes into account only the original table, and the
table to be merged. Child tables are not affected or included. If a
table has one or more child tables, defined as part of a relationship,
each child table must be merged individually.
When merging a new source DataTable into the target, any source rows
with a DataRowState value of Unchanged, Modified, or Deleted, is
matched to target rows with the same primary key values. Source rows
with a DataRowState value of Added are matched to new target rows with
the same primary key values as the new source rows.

You can use DataSet , Create a new DataSet and Add the multiple tables to it ,
For Eg-
DataSet Ds = new DataSet();
DataTable Dt1= new DataTable();
Ds.Tables.Add(Dt1)
you can add multiple tables and to access the datatable you can use the index ( eg -
Ds.Tables[0])
Hope this answers your question!!.

public DataSet Getdatasettables()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
return ds;
}

Related

Copy Datatable1 values to Datatable2 with the required columns configuration

Copy data from Datatable1 to Datattable2 only for specific columns. I have a method with List of column vlaues and Datatable as input. I need to get all the columns from input datatable and copy to new datatble.
/*Input:
columnNames in list : column1,column2,column3
datatable1 : column1,column2,column5,column6,column3
Output:
datatble2 : column1,column2,column3 (columns from list need to be copied from datatble1 to datatble2 and return datatble2)
*/
public DataTable CopyFromDatatbale(List<string> columnNames,DataTable datatable1)
{
DataTable datatble2=new DataTable();
/*
Code to copy the data from datatable1 to datatble2 with specific columns
*/
}
This is the code i am looking for...Copying data from DataTable1 to DataTable2 for specified columns .Finally in the datatable2 i have all the columns from datatble1 (Only specific columns)
//Copy Columns from Datatable1 to Datatble2 based on columns on columnList
DataView dtView = new DataView(dataTable1);
DataTable dataTable2= new DataTable();
var getColumnNamesCommaSeperated = columnList.Select(x => x.columnNames).ToArray();
dataTable2= dataTable1.Select().CopyToDataTable()
.DefaultView.ToTable(false, getColumnNamesCommaSeperated);

Compare Two Data tables values and add to new data table if different for add, update and delete

How can I add similar values from two DataTables A and B (same columns/rows may be different) to a new DataTable C for CRUD operations containing the similar values of first 2 Data Tables? Please advise.
Look like you don't put your effort to resolve it. Any there it is you can do to resolve your problem.
DataTable table1 = new DataTable();
table1.Columns.Add("MyId");
table1.Columns.Add("Column1");
table1.Columns.Add("Column2");
DataTable table2 = new DataTable();
table2.Columns.Add("Column3");
table2.Columns.Add("MyId");
table2.Columns.Add("Column4");
DataTable table3 = new DataTable();
table3.Columns.Add("Column5");
table3.Columns.Add("MyId");
table3.Columns.Add("Column6");
foreach (DataRow drtable1 in table1.Rows)
{
foreach (DataRow drtable2 in table2.Rows)
{
if (Convert.ToString(drtable1["MyId"]) == Convert.ToString(drtable2["MyId"]))
{
table3.Rows.Add(drtable1["MyId"], drtable1["Column1"], drtable1["Column2"]);
}
}
}

database not affected by dataset after renewing a table

I have a dataset that contains a table. This table has just a column (without primary key). My table is shown in a datagridview. Now, I want to edit data of table. So, I store data of table in a DataTable and then, I clear all data of table in dataset and fill it by DataTable, like this:
DataView dv = ((BindingSource)tblMyTableDataGridView.DataSource).SyncRoot as DataView;
DataTable dt = dv.ToTable();
IDataReader dr = dt.CreateDataReader();
myDataSet.tblMyTable.Clear();
myDataSet.tblMyTable.Load(dr);
By breaking point, that specified that tblMyTable has changed truely.
But it not affected on database. My code is:
tableAdapterManager.tblMyTableTableAdapter.Update(myDataSet.tblMyTable);
tableAdapterManager.UpdateAll(myDataSet);
What is wrong?!?!?
Thanks.

Distinct records in DataTable

I want to get distinct records based on some fields. I'm using the following method:
string[] TobeDistinct = { "PKID" };
DataTable dtDistinct = GetDistinctRecords(ds.Tables[0], TobeDistinct);
DataSet ds2 = new System.Data.DataSet();
ds2.Tables.Add(dtDistinct);
public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
{
DataTable dtUniqRecords = new DataTable();
dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
return dtUniqRecords;
}
This gives me the distinct records, but only two records come. Only two distinct PKID will come. For example, I have multiple records with PKID 10,12,14,16, but the result is 2 rows with PKID 10 and 12. More two rows are not there, but should be there. What do I need to do?
I follow this article: http://www.codeproject.com/Tips/153008/Select-DISTINCT-records-based-on-specified-fields
You can use like follows
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);
More detail
How to select distinct rows in a datatable and store into an array
Can you try this?
var myResult = dt.AsEnumerable().Select(c => (DataRow)c["MyColumn"]).Distinct().ToList();

how to copy only the columns in a DataTable to another DataTable?

how to copy only the columns in a DataTable to another DataTable?
DataTable.Clone() should do the trick.
DataTable newTable = originalTable.Clone();
If only the columns are required then DataTable.Clone() can be used. With Clone function only the schema will be copied. But DataTable.Copy() copies both the structure and data
E.g.
DataTable dt = new DataTable();
dt.Columns.Add("Column Name");
dt.Rows.Add("Column Data");
DataTable dt1 = dt.Clone();
DataTable dt2 = dt.Copy();
dt1 will have only the one column but dt2 will have one column with one row.
Datatable.Clone is slow for large tables. I'm currently using this:
Dim target As DataTable =
New DataView(source, "1=2", Nothing, DataViewRowState.CurrentRows)
.ToTable()
Note that this only copies the structure of source table, not the data.
If you want the structure of a particular data table(dataTable1) with column headers (without data) into another data table(dataTable2), you can follow the below code:
DataTable dataTable2 = dataTable1.Clone();
dataTable2.Clear();
Now you can fill dataTable2 according to your condition. :)
If you want to copy the DataTable to another DataTable of different Schema Structure then you can do this:
Firstly Clone the first DataType so that you can only get the structure.
Then alter the newly created structure as per your need and then copy the data to newly created DataTable
.
So:
Dim dt1 As New DataTable
dt1 = dtExcelData.Clone()
dt1.Columns(17).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(26).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(30).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(35).DataType = System.Type.GetType("System.Decimal")
dt1.Columns(38).DataType = System.Type.GetType("System.Decimal")
dt1 = dtprevious.Copy()
Hence you get the same DataTable but revised structure
The DataTable.Clone() method works great when you want to create a completely new DataTable, but there might be cases where you would want to add the schema columns from one DataTable to another existing DataTable.
For example, if you've derived a new subclass from DataTable, and want to import schema information into it, you couldn't use Clone().
E.g.:
public class CoolNewTable : DataTable {
public void FillFromReader(DbDataReader reader) {
// We want to get the schema information (i.e. columns) from the
// DbDataReader and
// import it into *this* DataTable, NOT a new one.
DataTable schema = reader.GetSchemaTable();
//GetSchemaTable() returns a DataTable with the columns we want.
ImportSchema(this, schema); // <--- how do we do this?
}
}
The answer is just to create new DataColumns in the existing DataTable using the schema table's columns as templates.
I.e. the code for ImportSchema would be something like this:
void ImportSchema(DataTable dest, DataTable source) {
foreach(var c in source.Columns)
dest.Columns.Add(c);
}
or, if you're using Linq:
void ImportSchema(DataTable dest, DataTable source) {
var cols = source.Columns.Cast<DataColumn>().ToArray();
dest.Columns.AddRange(cols);
}
This was just one example of a situation where you might want to copy schema/columns from one DataTable into another one without using Clone() to create a completely new DataTable. I'm sure I've come across several others as well.

Categories