Adding Expressions, Default Values and Constraints to a Table
2005-02-07 17:23
399 查看
DataTable objTable = new DataTable("NewBooks");
// declare a variable to hold a DataColumn object
DataColumn objColumn;
// define the columns (fields) within the table
// the first is an AutoIncrement column to act as the key
objColumn = objTable.Columns.Add("kBookKey", Type.GetType("System.Int32"));
objColumn.AutoIncrement = true;
objColumn.AutoIncrementSeed = 1000;
objColumn.AutoIncrementStep = 10;
// next, the ISBN column which must be unique and not allow Null values
objColumn = objTable.Columns.Add("ISBN", Type.GetType("System.String"));
objColumn.AllowDBNull = false;
objColumn.Unique = true;
objColumn.MaxLength = 10;
// now two more columns to hold general information
objTable.Columns.Add("Title", Type.GetType("System.String"));
objTable.Columns.Add("PublicationDate", Type.GetType("System.DateTime"));
// add two columns to hold stock and order quantities, with default values of zero
objColumn = objTable.Columns.Add("StockQty", Type.GetType("System.Int32"));
objColumn.DefaultValue = 0;
objColumn = objTable.Columns.Add("OrderedQty", Type.GetType("System.Int32"));
objColumn.DefaultValue = 0;
// add a column containing an expression showing the quantity availability
objColumn = objTable.Columns.Add("AvailableQty", Type.GetType("System.Int32"));
objColumn.Expression = "[StockQty] - [OrderedQty]";
// declare a variable to hold a DataRow object
DataRow objDataRow;
// create a new DataRow object instance in this table
objDataRow = objTable.NewRow();
// and fill in the values
objDataRow["ISBN"] = "1234567800";
objDataRow["Title"] = "Professional Video Recorder Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 03, 01);
objDataRow["StockQty"] = 3956;
objDataRow["OrderedQty"] = 450;
objTable.Rows.Add(objDataRow);
// repeat to add two more rows
objDataRow = objTable.NewRow();
objDataRow["ISBN"] = "1234567801";
objDataRow["Title"] = "Professional WAP Phone Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 06, 01);
objDataRow["StockQty"] = 329;
// note - no "OrderedQty" provided so default value used
objTable.Rows.Add(objDataRow);
objDataRow = objTable.NewRow();
objDataRow["ISBN"] = "1234567802";
objDataRow["Title"] = "Professional Radio Station Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 04, 01);
// note - no "StockQty" provided so default value used
objDataRow["OrderedQty"] = 1200;
objTable.Rows.Add(objDataRow);
// assign the DataTable's DefaultView object to the DataGrid control
dgrResult.DataSource = objTable.DefaultView;
dgrResult.DataBind(); // and bind (display) the data
// declare a variable to hold a DataColumn object
DataColumn objColumn;
// define the columns (fields) within the table
// the first is an AutoIncrement column to act as the key
objColumn = objTable.Columns.Add("kBookKey", Type.GetType("System.Int32"));
objColumn.AutoIncrement = true;
objColumn.AutoIncrementSeed = 1000;
objColumn.AutoIncrementStep = 10;
// next, the ISBN column which must be unique and not allow Null values
objColumn = objTable.Columns.Add("ISBN", Type.GetType("System.String"));
objColumn.AllowDBNull = false;
objColumn.Unique = true;
objColumn.MaxLength = 10;
// now two more columns to hold general information
objTable.Columns.Add("Title", Type.GetType("System.String"));
objTable.Columns.Add("PublicationDate", Type.GetType("System.DateTime"));
// add two columns to hold stock and order quantities, with default values of zero
objColumn = objTable.Columns.Add("StockQty", Type.GetType("System.Int32"));
objColumn.DefaultValue = 0;
objColumn = objTable.Columns.Add("OrderedQty", Type.GetType("System.Int32"));
objColumn.DefaultValue = 0;
// add a column containing an expression showing the quantity availability
objColumn = objTable.Columns.Add("AvailableQty", Type.GetType("System.Int32"));
objColumn.Expression = "[StockQty] - [OrderedQty]";
// declare a variable to hold a DataRow object
DataRow objDataRow;
// create a new DataRow object instance in this table
objDataRow = objTable.NewRow();
// and fill in the values
objDataRow["ISBN"] = "1234567800";
objDataRow["Title"] = "Professional Video Recorder Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 03, 01);
objDataRow["StockQty"] = 3956;
objDataRow["OrderedQty"] = 450;
objTable.Rows.Add(objDataRow);
// repeat to add two more rows
objDataRow = objTable.NewRow();
objDataRow["ISBN"] = "1234567801";
objDataRow["Title"] = "Professional WAP Phone Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 06, 01);
objDataRow["StockQty"] = 329;
// note - no "OrderedQty" provided so default value used
objTable.Rows.Add(objDataRow);
objDataRow = objTable.NewRow();
objDataRow["ISBN"] = "1234567802";
objDataRow["Title"] = "Professional Radio Station Programming";
objDataRow["PublicationDate"] = new DateTime(2001, 04, 01);
// note - no "StockQty" provided so default value used
objDataRow["OrderedQty"] = 1200;
objTable.Rows.Add(objDataRow);
// assign the DataTable's DefaultView object to the DataGrid control
dgrResult.DataSource = objTable.DefaultView;
dgrResult.DataBind(); // and bind (display) the data
相关文章推荐
- MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K
- MySQL WorkbenchYou are using safe update mode and you tried to update a table without a WHERE that u
- HBase异常:hbase-default.xml file seems to be for and old version of HBase的解决方法
- What is the difference between SET and SELECT when assigning values to variables, in T-SQL?
- Error Code: 1175 You are using safe update mode and you tried to update a table without a WHERE that
- Value Table Fixed Values and Check Table
- ASP.NET MVC 3 (Adding a New Field to the Movie Model and Table) (8/9)
- 171.View the Exhibit and examine the command used to create the ZONEDATA table.
- Cannot find table rule and default data source with logic table
- MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K
- Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE
- mysql 更新sql脚本: you are using safe update mode and you tried to update a table
- mysql错误:Error Code: 1175. You are using safe update mode and you tried to update a table……
- Cannot connect to default server host 'centos' - check pbs_server daemon and
- Default Values Table (C# Reference)
- LabVIEW 7.0 Resets Type Definition Constants in Applications to Default Values
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
- MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K
- 在数据库中执行删除delete语句时出现:You are using safe update mode and you tried to update a table without a WHERE
- Table 9-5 -- coeff_token mapping to TotalCoeff(coeff_token) and TrailingOnes(coeff_token)