Working with Data—Defining the Table Structure

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
Database Articles

Working with Data

© 2006 Jeffery Suddeth

Defining the Table Structure

The following code segment defines the Customer table structure. The CustID field is defined as an auto counter field and used as the primary key.

DataTable custTable = new DataTable();
custTable.TableName = "Customers";
 
// this will be the primary key
DataColumn custId = 
   new DataColumn("CustID", typeof(int));
custId.AutoIncrement = true;
custId.AutoIncrementSeed = 101;
custId.AutoIncrementStep = 1;
 
custTable.Columns.Add(custId);
 
// make this the primary key
custTable.PrimaryKey = new DataColumn[] { custId };
 
// add some fields for customer data
custTable.Columns.Add(new DataColumn("Name", typeof(string)));
custTable.Columns.Add(new DataColumn("Phone", typeof(string)));
custTable.Columns.Add(new DataColumn("Email", typeof(string)));


Previous_Page_.gif Next_Page_.gif




Personal tools