we discussed about the commonly used properties of DataColumn that allows you to create a data column with specific behavior to store data in each row of DataTable. You can create the DataTable schema similar to the tables in SQL database using the different properties of DataColumn. After the initialization step of ASP.Net DataColumn you can access its properties to specify the columnName, auto increment etc.
C# Code for Using ASP.Net DataColumn Properties
// Initialize DataColumnDataColumn myDataColumn = new DataColumn();
// AllowDBNull propertymyDataColumn.AllowDBNull = false;
// set AutoIncrement property to truemyDataColumn.AutoIncrement = true;
// set AutoIncrementSeed property equal to 1myDataColumn.AutoIncrementSeed = 1;
// set AutoIncrementStep property equal to 1myDataColumn.AutoIncrementStep = 1;
// set ColumnName property to specify the column namemyDataColumn.ColumnName = "auto_ID";
// set DataType property of the columnmyDataColumn.DataType = System.Type.GetType("System.Int32");
// set Unique property of DataColumn to true to allow unqiue value for this column in each rowmyDataColumn.Unique = true;
Above C# code shows the use of properties of DataColumn. It also shows how to create an AutoIncrement identity column using DataColumn properties. For AutoIncrement column you must specify theDataType as integer type as shown in above sample code. To store the string type data in DataColumn field you can use System.String as a value of DataType property.
For example:
myDataColumn = new DataColumn();
myDataColumn.ColumnName = "firstName";
myDataColumn.DataType = System.Type.GetType("System.String");
No comments:
Post a Comment