PS: Actually this place is just as a record of what I do not understand. If there are any mistakes or improper places, please correct me.
With regard to DataRow and DataColumn, the row and column of the DataTable are actually rows and columns, as two set elements of a DataTable exist, with a method like X and y in a coordinate system to determine a unique table value.
It was thought that two were equal in nature, but the unintended problem was that they were not nearly as simple as they were.
Use the SQL statement to build the table, but forget to write: Create TABLE testtable (ID int PRIMARY key,description Char (30)), and then with DataReader of course nothing will return.
View the table with Vs.net 2003 Server Explorer without prompting for an error. There are columns, but there is no row content, which means that such an "empty table" can exist. Then try changing the SQL statement: CREATE TABLE testtable, or CREATE TABLE testtable (), all prompts for syntax errors. This shows that the basis for the table is the column.
And by the way DataReader work, is a behavior element traversal process, where the data members are grouped according to the column, output is two nested loops:
foreach (DataRow i in dt. Rows)
{
foreach (DataColumn j in dt. Columns)
{
Console.Write ("\t{0}", I[j]);
}
Console.WriteLine ();
}
All rows are listed as structures, but the access is actually in the access row, the column is just the name of the location where the data resides, and can be abstracted like this:
struct ROW
{
Type column_1;
Type column_2;
...
Type Column_n;
}
This leads to a problem based on the above: rows and columns are added in the table.
1.DataRow of Add
More conventional, is to add elements, can be seen as the above row[] add an element.
The specific approach is probably as follows:
DataRow Newrow=dt. NewRow (); Call the DataTable dt Method NewRow () declaration to create a new row and get a DataRow instance
...
Newrow["(Column Name ...)"] ="..."; Write Data for DataRow
...
Dt. Rows.Add (NewRow); Add in DT
It's over.
2.DataColumn of Add
Seems to be simple too. In fact, there is no Newcolumn () method in dt.
Two ways of thinking:
A. Built inside the SQL, this idea is not discussed here for the time being, interested can try;
B. Find similar Add () method calls within the program;
It is necessary to explain the relationship between the two classes of DataColumn and DataColumnCollection:
In fact, the table builds a framework of DataColumnCollection, a collection of DataColumn that contains a set of Column objects, that is, to add a row to a table, you need to raise the datacolumncollection of the table frame with add () Method rather than adding the element as a traversal on the DT, because the column is actually traversed as a row-> column. The idea has, then the concrete realization is as follows:
DataColumnCollection Dcc=ds. tables["Employees"]. Columns; DataSet DS, returning DataColumnCollection object collection
DataColumn newcolumn=new DataColumn (); Create a new column
NEWCOLUMN=DCC. ADD ("FullName", System.Type.GetType ("System.String"), "firstname+ ' +lastname"); Inserts a column into the table employees, where the Datacolumncollection.add (string Columnname,type columntype,string Expression) method is used to insert the column. The expression here refers to the establishment of the new column values established by the rule
At this point, the column insertion ends.
As a small discussion, DataRow and DataColumn such a simple introduction, I hope to be a little help.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.