本文整理汇总了C#中Serenity.Data.SqlQuery.ForEach方法的典型用法代码示例。如果您正苦于以下问题:C# SqlQuery.ForEach方法的具体用法?C# SqlQuery.ForEach怎么用?C# SqlQuery.ForEach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Serenity.Data.SqlQuery
的用法示例。
在下文中一共展示了SqlQuery.ForEach方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTablePrimaryFields
public static List<string> GetTablePrimaryFields(IDbConnection connection, string schema, string tableName)
{
var inf = InformationSchema(connection);
List<string> primaryFields = new List<string>();
var columns = ((DbConnection)((WrappedConnection)connection).ActualConnection).GetSchema("Columns", new string[] { null, schema, tableName, null });
foreach (DataRow row in columns.Rows)
{
try
{
var isPrimaryKey = row["PRIMARY_KEY"] as Boolean?;
if (isPrimaryKey == true)
primaryFields.Add((string)row["COLUMN_NAME"]);
}
catch (Exception)
{
}
}
if (primaryFields.Count == 0)
{
var query = new SqlQuery().Select(
"KCU.COLUMN_NAME")
.From(
inf + "TABLE_CONSTRAINTS AS TC INNER JOIN " +
inf + "KEY_COLUMN_USAGE AS KCU " +
"ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA AND " +
"KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME AND " +
"KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA AND " +
"KCU.TABLE_NAME = TC.TABLE_NAME")
.Where(
new Criteria("TC.CONSTRAINT_TYPE") == "PRIMARY KEY" &
new Criteria("KCU.TABLE_NAME") == tableName)
.OrderBy(
"KCU.ORDINAL_POSITION");
query.ForEach(connection, delegate(IDataReader reader)
{
primaryFields.Add(reader.GetString(0));
});
}
return primaryFields;
}
示例2: List
public static AuditLogListResponse List(string schema, AuditLogListRequest request)
{
#if COREFX
var fld = Dependency.Resolve<IAuditLogRow>();
#else
var fld = Dependency.Resolve<IAuditLogRow>(schema);
#endif
Authorization.ValidateLoggedIn();
var response = new AuditLogListResponse();
using (var connection = SqlConnections.NewByKey("Default"))
{
response.Entities = new List<Row>();
var row = ((Row)fld).CreateNew();
if (request.Sort == null ||
request.Sort.Length == 0)
request.Sort = new SortBy[] { new SortBy(fld.DateField.Name, true) };
var query = new SqlQuery().From(row)
.Select(
(Field)fld.IdField,
fld.EntityTypeIdField,
fld.EntityIdField,
fld.ParentTypeIdField,
fld.OldParentIdField,
fld.NewParentIdField,
fld.DateField,
(Field)fld.UserIdField,
fld.AuditTypeIdField,
fld.OldAuditDataField,
fld.NewAuditDataField)
.OrderBy(
(Field)fld.IdField)
.ApplySkipTakeAndCount(request.Skip, request.Take, request.ExcludeTotalCount)
.ApplySort(request.Sort);
if (request.EntityTypeId != null &&
request.EntityId != null)
{
var pEntityId = query.AddParam(request.EntityId);
var pEntityTypeId = query.AddParam(request.EntityTypeId);
query.Where(~(
~(
new Criteria(0, fld.EntityTypeIdField) == pEntityTypeId &
new Criteria(0, fld.EntityIdField) == pEntityId) |
~(
new Criteria(0, fld.ParentTypeIdField) == pEntityTypeId &
~(
new Criteria(0, fld.OldParentIdField) == pEntityId |
new Criteria(0, fld.NewParentIdField) == pEntityId))));
}
else
{
if (request.EntityTypeId != null)
query.WhereEqual(fld.EntityTypeIdField, request.EntityTypeId);//Convert.ToInt32(request.EntityTypeId.Value));
if (request.EntityId != null)
query.WhereEqual(fld.EntityIdField, request.EntityId.Value);
}
response.TotalCount = query.ForEach(connection, delegate()
{
response.Entities.Add(row.Clone());
});
response.SetSkipTakeTotal(query);
response.IdNameLookups = new Dictionary<EntityType, Dictionary<long, string>>();
response.FieldTitles = new Dictionary<EntityType, Dictionary<string, string>>();
response.ForeignEntityTypes = new Dictionary<EntityType, Dictionary<string, string>>();
response.EntityTitles = new Dictionary<EntityType, string>();
var lookups = response.IdNameLookups;
var titles = response.FieldTitles;
var foreigns = response.ForeignEntityTypes;
var entities = response.EntityTitles;
Action<EntityType, Int64> addLookup = (entityType, id) => {
Dictionary<long, string> lookup;
if (!lookups.TryGetValue(entityType, out lookup))
{
lookup = new Dictionary<long, string>();
lookups[entityType] = lookup;
}
if (!lookup.ContainsKey(id))
lookup[id] = null;
};
Action<EntityType, string> addTitle = (entityType, field) =>
{
Dictionary<string, string> lookup;
if (!titles.TryGetValue(entityType, out lookup))
//.........这里部分代码省略.........
示例3: GetTablePrimaryFields
public static List<string> GetTablePrimaryFields(IDbConnection connection, string schema, string tableName)
{
var inf = InformationSchema(connection);
List<string> primaryFields = new List<string>();
if (connection.GetDialect().ServerType == "Firebird")
{
var query = @"
select
cast(RC.RDB$RELATION_NAME as varchar(31)) as TABLE_NAME,
cast(ISGMT.RDB$FIELD_NAME as varchar(31)) as COLUMN_NAME,
CAST((ISGMT.RDB$FIELD_POSITION + 1) as SMALLINT) as KEY_SEQ,
cast(RC.RDB$CONSTRAINT_NAME as varchar(31)) as PK_NAME
from
RDB$RELATION_CONSTRAINTS RC
INNER JOIN RDB$INDEX_SEGMENTS ISGMT ON RC.RDB$INDEX_NAME = ISGMT.RDB$INDEX_NAME
where CAST(RC.RDB$RELATION_NAME AS VARCHAR(40)) = '{0}' and
RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
order by 3";
foreach (var k in connection.Query(String.Format(query, tableName)))
{
primaryFields.Add(((string)k.COLUMN_NAME).TrimEnd());
}
return primaryFields;
}
var columns = ((DbConnection)((WrappedConnection)connection).ActualConnection).GetSchema("Columns", new string[] { null, schema, tableName, null });
if (columns.Columns.Contains("PRIMARY_KEY"))
{
foreach (DataRow row in columns.Rows)
{
try
{
var isPrimaryKey = row["PRIMARY_KEY"] as Boolean?;
if (isPrimaryKey == true)
primaryFields.Add((string)row["COLUMN_NAME"]);
}
catch (Exception)
{
}
}
return primaryFields;
}
if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase) ||
connection.GetDialect().ServerType.StartsWith("SqlServer", StringComparison.OrdinalIgnoreCase))
{
var query = new SqlQuery().Select(
"KCU.COLUMN_NAME")
.From(
inf + "TABLE_CONSTRAINTS AS TC INNER JOIN " +
inf + "KEY_COLUMN_USAGE AS KCU " +
"ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA AND " +
"KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME AND " +
"KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA AND " +
"KCU.TABLE_NAME = TC.TABLE_NAME")
.Where(
new Criteria("TC.CONSTRAINT_TYPE") == "PRIMARY KEY" &
new Criteria("KCU.TABLE_NAME") == tableName)
.OrderBy(
"KCU.ORDINAL_POSITION");
query.ForEach(connection, delegate(IDataReader reader)
{
primaryFields.Add(reader.GetString(0));
});
}
return primaryFields;
}
示例4: GetTablePrimaryFields
public static List<string> GetTablePrimaryFields(IDbConnection connection, string schema, string tableName)
{
var inf = InformationSchema(connection);
List<string> primaryFields = new List<string>();
if (connection.GetDialect().ServerType == "Firebird")
{
var q = @"
select
cast(RC.RDB$RELATION_NAME as varchar(31)) as TABLE_NAME,
cast(ISGMT.RDB$FIELD_NAME as varchar(31)) as COLUMN_NAME,
CAST((ISGMT.RDB$FIELD_POSITION + 1) as SMALLINT) as KEY_SEQ,
cast(RC.RDB$CONSTRAINT_NAME as varchar(31)) as PK_NAME
from
RDB$RELATION_CONSTRAINTS RC
INNER JOIN RDB$INDEX_SEGMENTS ISGMT ON RC.RDB$INDEX_NAME = ISGMT.RDB$INDEX_NAME
where CAST(RC.RDB$RELATION_NAME AS VARCHAR(40)) = '{0}' and
RC.RDB$CONSTRAINT_TYPE = 'PRIMARY KEY'
order by 3";
foreach (IDictionary<string, object> k in connection.Query(String.Format(q, tableName)))
{
primaryFields.Add((k["COLUMN_NAME"] as string).TrimEnd());
}
return primaryFields;
}
var query = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE [email protected] and TABLE_NAME = @tableName";
bool sqlite = connection.GetDialect().ServerType.StartsWith("Sqlite", StringComparison.OrdinalIgnoreCase);
if (sqlite)
query = "PRAGMA table_info(@tableName)";
var columns = connection.Query(query, new
{
schema,
tableName
});
foreach (IDictionary<string, object> column in columns)
{
try
{
var isPrimaryKey = column["PRIMARY_KEY"] as Boolean?;
if (isPrimaryKey == true)
primaryFields.Add(column["COLUMN_NAME"] as string);
}
catch
{
break;
}
return primaryFields;
}
if (connection.GetDialect().ServerType.StartsWith("MySql", StringComparison.OrdinalIgnoreCase) ||
connection.GetDialect().ServerType.StartsWith("SqlServer", StringComparison.OrdinalIgnoreCase))
{
var query2 = new SqlQuery().Select(
"KCU.COLUMN_NAME")
.From(
inf + "TABLE_CONSTRAINTS AS TC INNER JOIN " +
inf + "KEY_COLUMN_USAGE AS KCU " +
"ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA AND " +
"KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME AND " +
"KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA AND " +
"KCU.TABLE_NAME = TC.TABLE_NAME")
.Where(
new Criteria("TC.CONSTRAINT_TYPE") == "PRIMARY KEY" &
new Criteria("KCU.TABLE_NAME") == tableName)
.OrderBy(
"KCU.ORDINAL_POSITION");
query2.ForEach(connection, delegate(IDataReader reader)
{
primaryFields.Add(reader.GetString(0));
});
}
return primaryFields;
}