-
Detailed explanation of C# DataSet and DataTable
2009-Thursday-21
1. Create a DataSet object:
DataSet ds = new DataSet("DataSetName");
2. View the structure created by calling SqlDataAdapter.Fill
da.Fill(ds,"Orders");
DataTable tbl = ds.Table[zero];
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(col.ColumnName);
3. View the data returned by SqlDataAdapter ①, DataRow object
DataTable tbl = ds.Table[zero];
DataRow row = tbl.Row[zero];
Console.WriteLine(ros["OrderID"]);
②. Check the data stored in DataRow
DataTable tbl = row.Table;
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(row[col]);
③. Check the DataRow object in DatTable
foreach(DataRow row in tbl.Rows)
DisplayRow(row);
4. Verify the data in the DataSet ① Verify the properties of the DataColumn: ReadOnly, AllowDBNull, MaxLength, Unique
②. Constrains aggregation of DataTable objects: UiqueConstraints, Primarykey, ForeignkeyConstraints
Normally there is no need to bother creating ForeignkeyConstraints, because one is created when a relationship is created between two DataTable objects in a DataSet.
③. Use SqlDataAdapter.Fill method to retrieve method information
5. Compile code to create DataTable objects
①. Create a DataTable object: DataTable tbl = new DataTable("TableName");
②. Add the DataTable to the Table convergence of the DataSet object
DataSet ds = new DataSet();
DataTable tbl = new DataTable("Customers");
ds.Tables.Add(tbl);
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Customers");
DataTable objects can only exist in at most one DataSet object. If you want to add a DataTable to multiple DataSets, you must use the Copy step or the Clone step. The Copy step creates a new DataTable that has the same structure as the original DataTable and contains similar rows; the Clone step creates a new DataTable that has the same structure as the original DataTable but does not contain any rows.
③. Add columns to DataTable
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col =tbl.Columns.Add("OrderID",typeof(int));
col.AllowDBNull = false;
col.MaxLength = five;
col.Unique = true;
tbl.PrimaryKey = new DataColumn[]{tbl.Columns["CustomersID"]};
When a primary key should be set, AllowDBNull is automatically set to False;
④. Dispose of automatic increment columns
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col = tbl.Columns.Add("OrderID",typeof(int));
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
col.ReadOnly = true;
⑤. Add columns based on expressions
tbl.Columns.Add("ItemTotal",typeof(Decimal),"Quantity*UnitPrice");
6. Modify DataTable content ①, add new DataRow
DataRow row = ds.Tables["Customers"].NewRow();
row["CustomerID"] = "ALFKI";
ds.Tables["Customers"].Rows.Add(row);
object[] aValues ={"ALFKI","Alfreds","Anders","030-22222"};
da.Tables["Customers"].LoadDataRow(aValues,false);
②. Correction is currently in progress
Modifying the contents of a row does not automatically modify the corresponding content in the database. Modifications to the row are considered pending changes that will later be delivered to the database using the SqlDataAdapter object.
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null)
//Not searching for customers
else
{
rowCustomer["CompanyName"] = "NewCompanyName";
rowCustomer["ContactName"] = "NewContactName";
}
//Recommended to use this form
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null)
//Not searching for customers
else
{
rowCustomer.BeginEdit();
rowCustomer["CompanyName"] = "NewCompanyName";
rowCustomer["ContactName"] = "NewContactName";
rowCustomer.EndEdit();
}
//null means not to modify the data in this column
obejct[] aCustomer ={null, "NewCompanyName", "NewContactName", null}
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
③. Handle the null value of DataRow
//Check if it is empty
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
if(rowCustomer.IsNull("Phone"))
Console.WriteLine("It's Null");
else
Console.WriteLine("It's not Null");
//Grant null value
rowCustomer["Phone"] = DBNull.Value;
④. Eliminate DataRow
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.Delete();
⑤. Eliminate DataRow
DataRow rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
da.Tables["Customers"].Remove(rowCustomer);
perhaps
ds.Tables["Customers"].RemoveAt(intIndex);
⑥. Use DataRow.RowState properties: Unchanged, Detached, Added, Modified, Deleted
private void DemonstrateRowState()
{ // Run a function to create a DataTable with one column. DataTable myTable = MakeTable(); DataRow myRow;
// Create a new DataRow. myRow = myTable.NewRow(); // Detached row. Console.WriteLine("New Row " + myRow.RowState);
myTable.Rows.Add(myRow); // New row. Console.WriteLine("AddRow " + myRow.RowState);
myTable.AcceptChanges(); // Unchanged row. Console.WriteLine("AcceptChanges " + myRow.RowState);
myRow["FirstName"] = "Scott"; // Modified row. Console.WriteLine("Modified " + myRow.RowState);
myRow.Delete(); // Deleted row. Console.WriteLine("Deleted " + myRow.RowState); }
⑦. Check the pending changes in DataRow
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer["CompanyName"] = "NewCompanyName";
string strNewCompanyName, strOldCompanyName;
Console.WriteLine(rowCustomer["CompanyName", DataRowVersion.Current]);
Console.WriteLine(rowCustomer["CompanyName", DataRowVersion.Original]);
1. DataSet
①、Attributes
CaseSensitive: Used to control whether string comparisons in DataTable are case-sensitive.
TOP
Source of this article:
My abnormal network
JavaException
DotnetException
Oracle