本文整理汇总了C#中IDataTable.CreateIndex方法的典型用法代码示例。如果您正苦于以下问题:C# IDataTable.CreateIndex方法的具体用法?C# IDataTable.CreateIndex怎么用?C# IDataTable.CreateIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDataTable
的用法示例。
在下文中一共展示了IDataTable.CreateIndex方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateUniqueIndexForIdentity
/// <summary>给非主键的自增字段建立唯一索引</summary>
/// <param name="table"></param>
protected virtual void CreateUniqueIndexForIdentity(IDataTable table)
{
foreach (var dc in table.Columns)
{
if (dc.Identity && !dc.PrimaryKey)
{
var di = table.GetIndex(dc.Name);
if (di == null)
{
di = table.CreateIndex();
di.Columns = new String[] { dc.Name };
di.Computed = true;
}
// 不管是不是原来有的索引,都要唯一
di.Unique = true;
}
}
}
示例2: CreateIndexForRelation
/// <summary>给所有关系字段建立索引</summary>
/// <param name="table"></param>
protected virtual void CreateIndexForRelation(IDataTable table)
{
foreach (var dr in table.Relations)
{
// 跳过主键
var dc = table.GetColumn(dr.Column);
if (dc == null || dc.PrimaryKey) continue;
if (table.GetIndex(dr.Column) == null)
{
var di = table.CreateIndex();
di.Columns = new String[] { dr.Column };
// 这两个的关系,唯一性
di.Unique = dr.Unique;
di.Computed = true;
table.Indexes.Add(di);
}
}
}
示例3: CreateTableSQL
public override string CreateTableSQL(IDataTable table)
{
var sql = base.CreateTableSQL(table);
var pks = table.PrimaryKeys;
if (String.IsNullOrEmpty(sql) || pks.Length < 2) return sql;
// 处理多主键
var names = pks.Select(e => e.ColumnName).ToArray();
var di = ModelHelper.GetIndex(table, names);
if (di == null)
{
di = table.CreateIndex();
di.PrimaryKey = true;
di.Unique = true;
di.Columns = names;
}
// Access里面的主键索引名必须叫这个
di.Name = "PrimaryKey";
sql += ";" + Environment.NewLine;
sql += CreateIndexSQL(di);
return sql;
}
示例4: CreateTableSQL
public override string CreateTableSQL(IDataTable table)
{
String sql = base.CreateTableSQL(table);
if (String.IsNullOrEmpty(sql) || table.PrimaryKeys == null || table.PrimaryKeys.Length < 2) return sql;
// 处理多主键
String[] names = new String[table.PrimaryKeys.Length];
for (int i = 0; i < table.PrimaryKeys.Length; i++)
{
names[i] = table.PrimaryKeys[i].Name;
}
IDataIndex di = ModelHelper.GetIndex(table, names);
if (di == null)
{
di = table.CreateIndex();
di.PrimaryKey = true;
di.Unique = true;
di.Columns = names;
}
// Access里面的主键索引名必须叫这个
di.Name = "PrimaryKey";
sql += ";" + Environment.NewLine;
sql += CreateIndexSQL(di);
return sql;
}
示例5: GetIndexes
/// <summary>获取索引</summary>
/// <param name="table"></param>
/// <returns></returns>
protected virtual List<IDataIndex> GetIndexes(IDataTable table)
{
if (_indexes == null) return null;
DataRow[] drs = _indexes.Select(String.Format("{0}='{1}'", _.TalbeName, table.Name));
if (drs == null || drs.Length < 1) return null;
List<IDataIndex> list = new List<IDataIndex>();
foreach (DataRow dr in drs)
{
String name = null;
if (!TryGetDataRowValue<String>(dr, _.IndexName, out name)) continue;
IDataIndex di = table.CreateIndex();
di.Name = name;
if (TryGetDataRowValue<string>(dr, _.ColumnName, out name) && !String.IsNullOrEmpty(name))
di.Columns = name.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
else if (_indexColumns != null)
{
String orderby = null;
// Oracle数据库用ColumnPosition,其它数据库用OrdinalPosition
if (_indexColumns.Columns.Contains(_.OrdinalPosition))
orderby = _.OrdinalPosition;
else if (_indexColumns.Columns.Contains(_.ColumnPosition))
orderby = _.ColumnPosition;
DataRow[] dics = _indexColumns.Select(String.Format("{0}='{1}' And {2}='{3}'", _.TalbeName, table.Name, _.IndexName, di.Name), orderby);
if (dics != null && dics.Length > 0)
{
List<String> ns = new List<string>();
foreach (DataRow item in dics)
{
String dcname = null;
if (TryGetDataRowValue<String>(item, _.ColumnName, out dcname) &&
!String.IsNullOrEmpty(dcname) && !ns.Contains(dcname)) ns.Add(dcname);
}
if (ns.Count < 1) DAL.WriteDebugLog("表{0}的索引{1}无法取得字段列表!", table, di.Name);
di.Columns = ns.ToArray();
}
}
Boolean b = false;
if (TryGetDataRowValue<Boolean>(dr, "UNIQUE", out b))
di.Unique = b;
if (TryGetDataRowValue<Boolean>(dr, "PRIMARY", out b))
di.PrimaryKey = b;
else if (TryGetDataRowValue<Boolean>(dr, "PRIMARY_KEY", out b))
di.PrimaryKey = b;
FixIndex(di, dr);
list.Add(di);
}
return list != null && list.Count > 0 ? list : null;
}