本文整理汇总了C#中IGenerator.GenerateUpdateFromDataRow方法的典型用法代码示例。如果您正苦于以下问题:C# IGenerator.GenerateUpdateFromDataRow方法的具体用法?C# IGenerator.GenerateUpdateFromDataRow怎么用?C# IGenerator.GenerateUpdateFromDataRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IGenerator
的用法示例。
在下文中一共展示了IGenerator.GenerateUpdateFromDataRow方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDataDiffScript
//.........这里部分代码省略.........
where c.TableName == sourceTable
select c).ToList();
List<Column> targetColumns = (from c in targetRepository.GetAllColumns()
where c.TableName == targetTable
select c).ToList();
var sourcePkList = sourceRepository.GetAllPrimaryKeys().Where(pk => pk.TableName == sourceTable).Select(pk => pk.ColumnName).ToList();
if (sourcePkList.Count < 1)
{
throw new ArgumentException("Source does not have a primary key, this is required");
}
var targetPkList = targetRepository.GetAllPrimaryKeys().Where(pk => pk.TableName == targetTable).Select(pk => pk.ColumnName).ToList();
if (targetPkList.Count < 1)
{
throw new ArgumentException("Target does not have a primary key, this is required");
}
if (sourcePkList.Count != targetPkList.Count)
{
throw new ArgumentException("Source and Target primary key are not comparable, this is required");
}
if (sourceColumns.Count() != targetColumns.Count())
{
throw new ArgumentException("Source and target does not have same number of columns");
}
for (int i = 0; i < sourceColumns.Count(); i++)
{
if (sourceColumns[i].ShortType != targetColumns[i].ShortType)
{
throw new ArgumentException(string.Format("The columm {0} does not have the expected type {1} in the target table", sourceColumns[i].ColumnName, sourceColumns[i].ShortType));
}
}
string sourcePkSort = string.Empty;
string targetPkSort = string.Empty;
for(int i = 0; i < sourceColumns.Count(); i++)
{
if(sourcePkList.Contains(sourceColumns[i].ColumnName))
{
string prefix = (sourcePkSort == string.Empty) ? "" : ", ";
sourcePkSort += prefix + sourceColumns[i].ColumnName;
targetPkSort += prefix + targetColumns[i].ColumnName;
}
}
//two arrays in the same order, now just compare them
DataRow[] targetRows = targetRepository.GetDataFromTable(targetTable, targetColumns).Select(null, targetPkSort);
int targetRow = 0;
foreach(DataRow sourceRow in sourceRepository.GetDataFromTable(sourceTable, sourceColumns).Select(null, sourcePkSort))
{
//compare
int pkCompare = 0;
string whereClause = string.Empty;
if (targetRow < targetRows.Count())
{
for (int i = 0; i < sourcePkList.Count; i++)
{
if (whereClause.Length > 0)
whereClause += " AND ";
whereClause += String.Format(" [{0}] = {1}", targetPkList[i], generator.SqlFormatValue(targetTable, sourcePkList[i], targetRows[targetRow][sourcePkList[i]].ToString()));
}
if (whereClause.Length > 0)
whereClause += ";";
}
while (targetRow < targetRows.Count()
&& (pkCompare = CompareDataRows(sourceRow, sourcePkList, targetRows[targetRow], targetPkList)) > 0)
{
sb.AppendLine(String.Format("DELETE FROM [{0}] WHERE {1}", targetTable, whereClause));
sb.AppendLine("GO");
targetRow++;
whereClause = string.Empty;
for (int i = 0; i < sourcePkList.Count; i++)
{
if (whereClause.Length > 0)
whereClause += " AND ";
whereClause += String.Format(" [{0}] = {1}", targetPkList[i], generator.SqlFormatValue(targetTable, sourcePkList[i], targetRows[targetRow][sourcePkList[i]].ToString()));
}
if (whereClause.Length > 0)
whereClause += ";";
}
if (targetRow >= targetRows.Count() || pkCompare < 0)
{
sb.AppendLine(generator.GenerateInsertFromDataRow(targetTable, sourceRow));
targetRow++;
}
else if (CompareDataRows(sourceRow, null, targetRows[targetRow], null) != 0)
{
sb.AppendLine(String.Format("UPDATE [{0}] SET {1} WHERE {2}", targetTable, generator.GenerateUpdateFromDataRow(targetTable, sourceRow), whereClause));
sb.AppendLine("GO");
}
targetRow++;
}
return sb.ToString();
}