SQLServer数据库中的列对象 荣

基于http://www.cnblogs.com/admin11/archive/2007/09/01/878298.html 和 http://www.cnblogs.com/admin11/archive/2007/09/05/882898.html 中对数据库系统数据的读取,我们可以建立列对象。
我的列对象结构:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 数据库列信息类。
/// </summary>
/// <author>天志</author>
/// <log date="2007-09-01">创建</log>
public class ColumnDT
{
    
public ColumnDT()
    {
    }

    
public ColumnDT(DataRowView dv)
    {
        
this._tableName = Convert.ToString(dv["tableName"]);
        
this._id = Convert.ToInt32(dv["id"]);
        
this._columnName = Convert.ToString(dv["columnName"]);
        
this._colId = Convert.ToInt32(dv["colId"]);
        
this._columnType = Convert.ToString(dv["columnType"]);
        
this._columnTypeIndex = Convert.ToInt32(dv["columnTypeIndex"]);
        
this._length = Convert.ToInt32(dv["length"]);
        
this._decimaldigits = Convert.ToInt32(dv["decimaldigits"]);
        
this._script = Convert.ToString(dv["script"]);
        
this._defaultValue = Convert.ToString(dv["defaultValue"]);
        
this._isnullable = Convert.ToInt32(dv["isnullable"]);
        
this._isMarking = Convert.ToInt32(dv["isMarking"]);
    }

    
/// <summary>
    
/// 所属表名称
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private string _tableName;

    
/// <summary>
    
/// 所属表ID
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _id;

    
/// <summary>
    
/// 列名
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private string _columnName;

    
/// <summary>
    
/// 列ID
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _colId;

    
/// <summary>
    
/// 数据类型
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private string _columnType;

    
/// <summary>
    
/// 数据类型索引
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _columnTypeIndex;

    
/// <summary>
    
/// 字段长度
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _length;

    
/// <summary>
    
/// 小数点位数
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _decimaldigits;

    
/// <summary>
    
/// 描述信息
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private string _script;

    
/// <summary>
    
/// 默认值
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private string _defaultValue;

    
/// <summary>
    
/// 是否允许为空
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    private int _isnullable;

    
/// <summary>
    
/// 是否标识
    
/// </summary>
    private int _isMarking;

    
/// <summary>
    
/// 所属表名称
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public string TableName
    {
        
set
        {
            
this._tableName = value;
        }
        
get
        {
            
return this._tableName;
        }
    }

    
/// <summary>
    
/// 所属表ID
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public int Id
    {
        
set
        {
            
this._id = value;
        }
        
get
        {
            
return this._id;
        }
    }

    
/// <summary>
    
/// 列名
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public string ColumnName
    {
        
set
        {
            
this._columnName = value;
        }
        
get
        {
            
return this._columnName;
        }
    }

    
/// <summary>
    
/// 列ID
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public int ColId
    {
        
set
        {
            
this._colId = value;
        }
        
get
        {
            
return this._colId;
        }
    }

    
/// <summary>
    
/// 数据类型
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public string ColumnType
    {
        
set
        {
            
this._columnType = value;
        }
        
get
        {
            
return this._columnType;
        }
    }

    
/// <summary>
    
/// 数据类型索引
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public int ColumnTypeIndex
    {
        
set
        {
            
this._columnTypeIndex = value;
        }
        
get
        {
            
return this._columnTypeIndex;
        }
    }

    
/// <summary>
    
/// 字段长度
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public int Length
    {
        
set
        {
            
this._length = value;
        }
        
get
        {
            
return this._length;
        }
    }

    
/// <summary>
    
/// 小数点位数
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public int Decimaldigits
    {
        
set
        {
            
this._decimaldigits = value;
        }
        
get
        {
            
return this._decimaldigits;
        }
    }

    
/// <summary>
    
/// 描述信息
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public string Script
    {
        
set
        {
            
this._script = value;
        }
        
get
        {
            
return this._script;
        }
    }

    
/// <summary>
    
/// 默认值
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public string DefaultValue
    {
        
set
        {
            
this._defaultValue = value;
        }
        
get
        {
            
return this._defaultValue;
        }
    }

    
/// <summary>
    
/// 是否允许为空
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public bool IsNullable
    {
        
set
        {
            
if (value)
            {
                
this._isnullable = 1;
            }
            
else
            {
                
this._isnullable = 0;
            }
        }
        
get
        {
            
if (this._isnullable == 1)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }
    }

    
/// <summary>
    
/// 是否标识
    
/// </summary>
    
/// <author>天志</author>
    
/// <log date="2007-09-01">创建</log>
    public bool IsMarking
    {
        
set
        {
            
if (value)
            {
                
this._isMarking = 1;
            }
            
else
            {
                
this._isMarking = 0;
            }
        }
        
get
        {
            
if (this._isMarking == 1)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }
    }

    
/// <summary>
    
/// 是否主键
    
/// </summary>
    public bool IsPK
    {
        
get
        {
            
return DataMarket.IsPKForColumn(this.TableName, this.ColumnName);
        }
    }
}


这个对象里面收集了一个列的所属表名称、所属表ID、列名、列ID、数据类型、数据类型索引、字段长度、小数点位数、描述信息、默认值、是否允许为空、是否标识(自增ID)、是否主键等信息。
其他的信息好得到。
是否主键信息有点困难,通过DataMarket.IsPKForColumn方法实现。DataMarket是一个类。提供数据操作静态方法的类。数据是从SQL-server数据库中取得的数据库信息。
IsPKForColumn以通过调用列名、所属表名称为参数,通过调用存储过程sp_pkeys来判断当前表的主键中是否包含该列名来判断该列是否是主键。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// 存储列信息的集合.
/// </summary>
/// <author>天志</author>
/// <log date="2007-09-01">创建</log>
public class ColumnList<T> : List<T> where T : ColumnDT
{
    
public ColumnList() : base()
    {
    }

    
public ColumnList(IEnumerable<T> collection)
        : 
base(collection)
    {
    }

    
public ColumnList(int capacity)
        : 
base(capacity)
    {
    }
}
原文地址:https://www.cnblogs.com/admin11/p/896849.html