zoukankan      html  css  js  c++  java
  • C# DataColumn

      DataColumn 是用来模拟物理数据库中的列。DataColumn 的组合组成了 DataTable 中列的架构。生成 DataTable 架构的方法就是向 DataColumnCollection 中添加DataColumn 对象来生成架构。同物理数据库一样,列是有类型的,比如 varchar, datatime, int 等, DataColumn 有 DataType 属性表示这一列所存储的数据种类。由于 DataTable 所包含的数据通常合并回其原始数据源,因此必须使其数据类型与数据源中的数据类型匹配。这个匹配关系,可以再 msdn 中的 《数据类型映射 (ADO.NET)》章节查询到。

      在物理数据库中,我们的列都要有各种约束来维持数据完整性,比如非空、唯一,同时也有各种自动化的操作,比如,自增。同样的在内存中,我们也可以这样定义,通过 AllowDBNull 、Unique 和 ReadOnly 等属性对数据的输入和更新施加限制,通过 AutoIncrement、AutoIncrementSeed 和 AutoIncrementStep 属性来实现数据自动生成。


    1.通过 DataColumn 创建列

      下面的示例用几个 DataColumn 对象创建 DataTable

    private void MakeTable()
    { 
        // Create a DataTable. 
        DataTable table = new DataTable("Product");
    
        // Create a DataColumn and set various properties. 
        DataColumn column = new DataColumn(); 
        column.DataType = System.Type.GetType("System.Decimal"); 
        column.AllowDBNull = false; 
        column.Caption = "Price"; 
        column.ColumnName = "Price"; 
        column.DefaultValue = 25; 
    
        // Add the column to the table. 
        table.Columns.Add(column); 
    
        // Add 10 rows and set values. 
        DataRow row; 
        for(int i = 0; i < 10; i++)
        { 
            row = table.NewRow(); 
            row["Price"] = i + 1; 
    
            // Be sure to add the new row to the 
            // DataRowCollection. 
            table.Rows.Add(row); 
        } 
    }

     2. 向数据表中添加列

       DataColumn 的主要作用就是添加到 DataTable 中。添加的方法有两个:1. 显示的使用构造函数,然后将引用添加到集合中。2. 表内创建 DataColumn 对象,也就是在集合中直接添加。以下示例向 DataTable 中添加了四列。

    DataTable workTable = new DataTable("Customers");
    
    DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
    workCol.AllowDBNull = false;
    workCol.Unique = true;
    
    workTable.Columns.Add("CustLName", typeof(String));
    workTable.Columns.Add("CustFName", typeof(String));
    workTable.Columns.Add("Purchases", typeof(Double));

    3. 定义主键

      在将一个单独的 DataColumn 标识为 DataTable 的 PrimaryKey 时,表会自动将列的 AllowDBNull 属性设置为 false,并将 Unique 属性设置为 true。 如果是多列主键,则只有 AllowDBNull 属性自动设置为 false。 

    //一列
    workTable.PrimaryKey = new DataColumn[] {workTable.Columns["CustID"]};
    
    // Or
    
    DataColumn[] columns = new DataColumn[1];
    columns[0] = workTable.Columns["CustID"];
    workTable.PrimaryKey = columns;
    //多列
    workTable.PrimaryKey = new DataColumn[] {workTable.Columns["CustLName"], 
                                             workTable.Columns["CustFName"]};
    
    // Or
    
    DataColumn[] keyColumn = new DataColumn[2];
    keyColumn[0] = workTable.Columns["CustLName"];
    keyColumn[1] = workTable.Columns["CustFName"];
    workTable.PrimaryKey = keyColumn;

    4.创建 AutoIncrement 列

      只要将 AutoIncrement 属性设置为 true。此列的值就会自动递增了。可以设置递增的初始值,和步进大小。同时,要把 ReadOnly 属性设置为 true。

    DataColumn workColumn = workTable.Columns.Add(
        "CustomerID", typeof(Int32));
    workColumn.AutoIncrement = true;
    workColumn.AutoIncrementSeed = 200;
    workColumn.AutoIncrementStep = 3;


    参考文献:http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn(v=vs.90)



  • 相关阅读:
    webdriver学习
    [Sqlite]-->Java使用jdbc连接Sqlite数据库进行各种数据操作的详细过程(转)
    java 二维码
    java 解析json超大文件(转)
    嵌套三目运算符
    实体的字段以is开头的教训
    easyui中formatter的使用
    springmvc中的controller是单例的
    hibernate 中baseservice中添加事物
    easyui中添加富文本编辑器
  • 原文地址:https://www.cnblogs.com/fengkuangshubiaodian/p/2609587.html
Copyright © 2011-2022 走看看