I have denormalized data in a DataTable.
The data contains employee names, and the pay they got over a series of pay cycles. i.e.:
My DataTable contains:
Employee 1 Jan-1-2012 $100
Employee 2 Jan-1-2012 $300
Employee 1 Feb-1-2012 $400
Employee 2 Feb-1-2012 $200
Employee 1 Mar-1-2012 $150
Employee 2 Mar-1-2012 $325
How can load this data into a DataSet where the parent DataTable contains the employees name, and the child DataTable contains details of the paycheck?
DataSet is nothing but a collection of DataTables. So to "load" the dataTable into dataSet simple Add it:
DataTable employees = new DataTable();
DataTable payCheckes = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(employees);
ds.Tables.Add(payCheckes);
Do you want to "combine" datatables somehow?
Get paycheckes of each employee?
the code without manual insert:
DataSet ds = new DataSet();
DataTable dtemploye = new DataTable();
DataTable dtpayment = new DataTable();
ds.Tables.AddRange(new DataTable[] { dtemploye, dtpayment });
DataColumn dcIdemploye = dtemploye.Columns["ID_EMPLOYEE"];
DataColumn dcIdemployeprice = dtpayment.Columns["ID_EMPLOYEE"];
DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
ds.Relations.Add(drrelation);
DataSet ds = new DataSet();
DataTable dtemploye = new DataTable();
DataColumn dcnameemploye = new DataColumn();
DataColumn dcIdemploye = new DataColumn();
dtemploye.Columns.AddRange(new DataColumn[]{dcnameemploye,dcIdemploye});
DataTable dtpayment = new DataTable();
DataColumn dtprice = new DataColumn();
DataColumn dtDate = new DataColumn();
DataColumn dcIdemployeprice = new DataColumn();
dtpayment.Columns.AddRange(new DataColumn[]{dcIdemployeprice,dtprice,dtDate});
DataRow drrowemploy = dtemploye.NewRow();
drrowemploy[0] = "1";
drrowemploy[1] = "Employee 1";
dtemploye.Rows.Add(drrowemploy);
DataRow drrowpayment = dtpayment.NewRow();
drrowpayment[0] = "1";
drrowpayment[0] = "01/01/2012";
drrowpayment[1] = " 300";
ds.Tables.AddRange(new DataTable[]{dtemploye, dtpayment});
DataRelation drrelation = new DataRelation("relemploy_payment", dcIdemploye, dcIdemployeprice);
ds.Relations.Add(drrelation);
Related
I have a small problem with creating a new table in a database. I create a new table in my DataSet object, but this table is not created in the physical database. The table is created only in cache. Why?
My code:
SqlCeConnection con = new SqlCeConnection(#"Data Source=C:\Projects\ConsoleApplication6\ConsoleApplication6\Data.sdf");
SqlCeDataAdapter sCEdata = new SqlCeDataAdapter("select * from [Cats]", con);
DataSet ds = new DataSet();
sCEdata.Fill(ds);
DataColumn ID = new DataColumn("ID", typeof(int));
ID.AllowDBNull = false;
ID.AutoIncrementSeed = 0;
ID.AutoIncrement = true;
ID.AutoIncrementStep = 1;
ID.ReadOnly = true;
ID.Unique = true;
DataColumn Name = new DataColumn("Name", typeof(string));
DataColumn Owner = new DataColumn("Owner", typeof(string));
DataColumn Note = new DataColumn("Note", typeof(string));
DataTable Cats2 = new DataTable("Cats2");
Cats2.Columns.AddRange(new DataColumn[]{ID, Name, Hozain, Note});
DataRow dr1 = Cats2.NewRow();
DataRow dr2 = Cats2.NewRow();
DataRow dr3 = Cats2.NewRow();
dr1["Name"] = "Pavel"; dr1["Owner"] = "Sergey"; dr1["Note"] = "Starii";
dr2["Name"] = "Gleb"; dr2["Owner"] = "Inga"; dr2["Note"] = "Tupaya";
dr3["Name"] = "Dusia"; dr3["Owner"] = "Olga"; dr3["Note"] = "Zlaya";
Cats2.Rows.Add(dr1);
Cats2.Rows.Add(dr2);
Cats2.Rows.Add(dr3);
dr2["Note"] = "Guzelle";
ds.Tables.Add(Cats2);
SqlCeCommandBuilder build = new SqlCeCommandBuilder(sCEdata);
sCEdata.Update(ds);
The Database only knows that is has to change something when you use a SQL command. You can create as many DataSets as you like, delete and insert data, but the Database stays the same until you tell it to change via a SQL command.
I have a dataset contains 2 datatables with a relation between them,
in the first table i have an ID (primary key) and MainAreaName and in the second table I have the ID from the first table with 2 other columns
the question is: how to view the MainAreaName from the first table with the other 2 columns from the second table in a gridview
DataSet MyDB = new DataSet("MyDB");
DataTable MainArea = new DataTable("MainArea");
DataTable NameNumber = new DataTable("NameNumber");
DataColumn Col01 = new DataColumn("ID", typeof(int));
Col01.AutoIncrement = true;
Col01.AutoIncrementSeed = 1;
Col01.AutoIncrementStep = 1;
DataColumn Col02 = new DataColumn("MainAreaName", typeof(string));
MainArea.Columns.Add(Col01);
MainArea.Columns.Add(Col02);
MainArea.PrimaryKey =
new DataColumn[] { MainArea.Columns["ID"] };
DataRow MyRow = MainArea.NewRow();
MyRow["MainAreaName"] = "Area1";
MainArea.Rows.Add(MyRow);
MyRow = MainArea.NewRow();
MyRow["MainAreaName"] = "Area2";
MainArea.Rows.Add(MyRow);
DataColumn Col11 = new DataColumn("MainAreaID", typeof(int));
DataColumn Col12 = new DataColumn("Name", typeof(string));
DataColumn Col13 = new DataColumn("Number", typeof(int));
DataColumn Col14 = new DataColumn("Comment", typeof(string));
NameNumber.Columns.Add(Col11);
NameNumber.Columns.Add(Col12);
NameNumber.Columns.Add(Col13);
NameNumber.Columns.Add(Col14);
NameNumber.PrimaryKey =
new DataColumn[] {
NameNumber.Columns["MainAreaID"], NameNumber.Columns["Number"] };
NameNumber.Rows.Add(1, "Test1", 67);
NameNumber.Rows.Add(1, "Test2", 87);
NameNumber.Rows.Add(2, "Test3", 77);
NameNumber.Rows.Add(2, "Test4", 88);
MyDB.Tables.Add(MainArea);
MyDB.Tables.Add(NameNumber);
MyDB.Relations.Add(
MyDB.Tables["MainArea"].Columns["ID"],
MyDB.Tables["NameNumber"].Columns["MainAreaID"]);
I tried to use the following code
DataRelation DR = new DataRelation("MainAreaRelation", MyDB.Tables["MainArea"].Columns["ID"], MyDB.Tables["NameNumber"].Columns["MainAreaID"]);
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable;
//or
dataGridView1.DataSource = MyDB.Relations["MainAreaRelation"].ParentTable;
but didn't work,
anyone can help please?
i have this making table:
DataTable WorkTbl()
{
DataTable Work= new DataTable("Work"); //Table Name
DataColumn MAC = new DataColumn("MAC", typeof(string));
DataColumn ID_OLD = new DataColumn("ID_OLD", typeof(string));
Work.Columns.Add(MAC);
Work.Columns.Add(ID_OLD);
return Work;
}
how to insert data to this table and how to convert this table to Dataset ?
thanks in advance
From MSDN:
DataRow workRow = workTable.NewRow();
You then can manipulate the newly added row using an index or the column name, as shown in the following example.
workRow["CustLName"] = "Smith";
workRow[1] = "Smith";
DataSet customerOrders = new DataSet("CustomerOrders");
DataTable ordersTable = customerOrders.Tables.Add("Orders");
DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));
ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };
i need to set a table to a dataset
DataSet ds = EventDal.GetEvents();
DataSet dsReturn = new DataSet();
DataTable dtReturn = dsReturn.Tables.Add();
dtReturn.Columns.Add("id");
dtReturn.Columns.Add("description");
dtReturn.Columns.Add("status");
foreach (DataRow row in ds.Tables[0].Rows)
{
if(Convert.ToInt32(row[1]) == status )
{
DataRow newrowdata = dtReturn.NewRow();
dsReturn.Tables["dtReturn"].ImportRow((row));///i'm getting object ref not set to an instance of object..
//DataRow drReturn = dtReturn.NewRow();
//dsReturn.Tables["dtReturn"].Rows.Add(row);
}
}
return dsReturn;
DataSet customerOrders = new DataSet("CustomerOrders");
DataTable ordersTable = customerOrders.Tables.Add("Orders");
DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));
ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };
I first want to add a database column in DataColumn..and then i have to add datacoloumn in datatable.. please guide me;
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
Is the bit you're missing
DataTable DT = new DataTable;
d2.Fill(DT);
DT.Columns.Add(c);
???
If you wish to create a DataTable, and add your own columns to it, you can try
DataTable dt = new DataTable("MyTable");
DataColumn col = dt.Columns.Add("parta", typeof(int));
You only have to keep a reference to the column if you plan to use it somewhere, else you can try
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("parta", typeof(int));
I would suggest that you use the SqlDataAdapter to populate a DataSet, which will contain a DataTable. I assume you want to add the new column to this DataTable.
Here's how to do that:
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataSet dst = new DataSet();
d2.Fill(dst); // now you have a populated DataSet containing a DataTable
DataTable dt = dst.Tables[0]; // I created this variable for clarity; you don't really need it
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
dt.Add(c);
After you add the Column to the DataTable, the Column will be empty.
You can populate it in code.