一个对记录类型数据的封装,类似表结构封装
调试环境:Visual Stdio 2005,C#.net
/// <summary>
/// 记录类型数据(表结构)
/// </summary>
public class DBRecordInfo
{
private IList<DBFieldInfo> fields;
protected string tableName;
protected string[] fieldNameItem;
public DBRecordInfo()
{
this.fields = new List<DBFieldInfo>();
GetTableMsg();
}
public DBRecordInfo(IList<DBFieldInfo> fields)
{
this.fields = fields;
GetTableMsg();
}
/// <summary>
/// 增加字段
/// </summary>
public bool Add(DBFieldInfo field)
{
try
{
this[field.FieldName] = field;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 删除字段
/// </summary>
public bool Del(DBFieldInfo field)
{
try
{
fields.Remove(field);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 根据字段名访问字段
/// </summary>
public DBFieldInfo this[string fieldName]
{
get
{
foreach (DBFieldInfo infield in fields)
{
if (infield.FieldName == fieldName)
return infield;
}
return null;
}
set
{
for (int index = 0; fields != null && index < fields.Count; index++)
{
if (fields[index].FieldName == fieldName)
{
fields[index] = value;
return;
}
}
fields.Add(value);
}
}
/// <summary>
/// 根据索引访问字段
/// </summary>
public DBFieldInfo this[int index]
{
get
{
if (index < fields.Count)
return fields[index];
else
return null;
}
set
{
if (index < fields.Count)
fields[index] = value;
else
{
//throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// 字段个数,只读
/// </summary>
public int Length
{
get
{
if (fields == null)
return 0;
return fields.Count;
}
}
/// <summary>
/// 字段名称集合,只读属性
/// </summary>
public string[] FieldNameItem
{
get
{
return fieldNameItem;
}
}
/// <summary>
/// 表名称,只读属性
/// </summary>
public string TableName
{
get
{
return tableName;
}
}
/// <summary>
/// 获取表信息
/// 此处为构造临时表,子类可以重写此方法
/// </summary>
virtual protected void GetTableMsg()
{
tableName = "TempTable";
fieldNameItem = null;
}
}
调试环境:Visual Stdio 2005,C#.net
/// <summary>
/// 记录类型数据(表结构)
/// </summary>
public class DBRecordInfo
{
private IList<DBFieldInfo> fields;
protected string tableName;
protected string[] fieldNameItem;
public DBRecordInfo()
{
this.fields = new List<DBFieldInfo>();
GetTableMsg();
}
public DBRecordInfo(IList<DBFieldInfo> fields)
{
this.fields = fields;
GetTableMsg();
}
/// <summary>
/// 增加字段
/// </summary>
public bool Add(DBFieldInfo field)
{
try
{
this[field.FieldName] = field;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 删除字段
/// </summary>
public bool Del(DBFieldInfo field)
{
try
{
fields.Remove(field);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 根据字段名访问字段
/// </summary>
public DBFieldInfo this[string fieldName]
{
get
{
foreach (DBFieldInfo infield in fields)
{
if (infield.FieldName == fieldName)
return infield;
}
return null;
}
set
{
for (int index = 0; fields != null && index < fields.Count; index++)
{
if (fields[index].FieldName == fieldName)
{
fields[index] = value;
return;
}
}
fields.Add(value);
}
}
/// <summary>
/// 根据索引访问字段
/// </summary>
public DBFieldInfo this[int index]
{
get
{
if (index < fields.Count)
return fields[index];
else
return null;
}
set
{
if (index < fields.Count)
fields[index] = value;
else
{
//throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// 字段个数,只读
/// </summary>
public int Length
{
get
{
if (fields == null)
return 0;
return fields.Count;
}
}
/// <summary>
/// 字段名称集合,只读属性
/// </summary>
public string[] FieldNameItem
{
get
{
return fieldNameItem;
}
}
/// <summary>
/// 表名称,只读属性
/// </summary>
public string TableName
{
get
{
return tableName;
}
}
/// <summary>
/// 获取表信息
/// 此处为构造临时表,子类可以重写此方法
/// </summary>
virtual protected void GetTableMsg()
{
tableName = "TempTable";
fieldNameItem = null;
}
}
本文介绍了一个用于封装记录类型数据的 C# 类 `DBRecordInfo`,它模拟了表结构,包含增加、删除字段的方法,并提供了按字段名和索引访问字段的功能。类中还包括了只读属性如字段个数、字段名称集合和表名称。

被折叠的 条评论
为什么被折叠?



