gpt4 book ai didi

c# - 使用反射 (C#) 更新通用 DataTable 和 TableAdaptor

转载 作者:行者123 更新时间:2023-11-30 22:49:17 25 4
gpt4 key购买 nike

我的应用程序中有几个强类型数据集。编写更新数据的方法越来越乏味,因为每个方法都有多个表。我想创建一个可以轻松更新所有表的通用函数。我不介意是否必须为每个 DataSet 创建其中一个,但如果一个函数可以处理所有这些,那就太棒了!

将有任意数量的新的、更新的或删除的记录,并且每一行都应该被正确标记。这个函数应该只是处理实际的保存。这是我目前所拥有的:

    private bool SaveData(object oTableAdaptor, object ds)
{
try
{
Type oType = oTableAdaptor.GetType();
MethodInfo[] oMethodInfoArray = oType.GetMethods();

foreach (MethodInfo oMI in oMethodInfoArray)
{
if (oMI.Name == "Update")
{
ParameterInfo[] oParamaterInfoArray = oMI.GetParameters();
foreach (ParameterInfo oPI in oParamaterInfoArray)
{
Type DsType = null;

if (oPI.ParameterType.Name == "NameOfDataSet")
{
DsType = typeof(MyDataSet);

// get a list of the changed tables???
}

if (((DataSet)ds).HasChanges() == true)
{
if (oPI.ParameterType == DsType)
{
object[] values = { ds };
try
{
oMI.Invoke(oTableAdaptor, values);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(oTableAdaptor.GetType().Name + Environment.NewLine + ex.Message);
}
}
}

}
}
}
}
catch (Exception Exp)
{
System.Diagnostics.Debug.WriteLine(Exp.Message);
if (Exp.InnerException != null) System.Diagnostics.Debug.WriteLine(Exp.InnerException.Message);

return false;
}

return true;

我从另一位开发人员在不同应用程序中的另一段代码中改编了它。到目前为止的主要区别是他传入了一个(对象类型的)数据适配器数组,并将三个数据集(全局实例化)中的每一个都设置为单独的 if block 内的 foreach(ParameterInfo oPI in oParamaterInfoArray) block (其中我的' NameOfDataSet' 将是数据集之一)

任何人都可以在完成此功能的方向上给我一点插入(或插入?)吗?我知道我就在那里,但感觉就像我在看什么东西。这段代码编译没有错误。

最佳答案

我一直在用这个。虽然它需要一些优化。这还负责根据数据集中的关系以正确的顺序更新表(如果没有自引用,可以通过对行进行排序来处理,但为简单起见,我不会在此处发布)。

    public static void Save(DataSet data, SqlConnection connection)
{
/// Dictionary for associating adapters to tables.
Dictionary<DataTable, SqlDataAdapter> adapters = new Dictionary<DataTable, SqlDataAdapter>();

foreach (DataTable table in data.Tables)
{
/// Find the table adapter using Reflection.
Type adapterType = GetTableAdapterType(table);
SqlDataAdapter adapter = SetupTableAdapter(adapterType, connection, validityEnd);
adapters.Add(table, adapter);
}

/// Save the data.
Save(data, adapters);
}

static Type GetTableAdapterType(DataTable table)
{
/// Find the adapter type for the table using the namespace conventions generated by dataset code generator.
string nameSpace = table.GetType().Namespace;
string adapterTypeName = nameSpace + "." + table.DataSet.DataSetName + "TableAdapters." + table.TableName + "TableAdapter";
Type adapterType = Type.GetType(adapterTypeName);
return adapterType;
}

static SqlDataAdapter SetupTableAdapter(Type adapterType, SqlConnection connection)
{
/// Set connection to TableAdapter and extract SqlDataAdapter (which is private anyway).
object adapterObj = Activator.CreateInstance(adapterType);
SqlDataAdapter sqlAdapter = (SqlDataAdapter)GetPropertyValue(adapterType, adapterObj, "Adapter");
SetPropertyValue(adapterType, adapterObj, "Connection", connection);

return sqlAdapter;
}

static object GetPropertyValue(Type type, object instance, string propertyName)
{
return type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(instance, null);
}

static void SetPropertyValue(Type type, object instance, string propertyName, object propertyValue)
{
type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance).SetValue(instance, propertyValue, null);
}

static void Save(DataSet data, Dictionary<DataTable, SqlDataAdapter> adapters)
{
if (data == null)
throw new ArgumentNullException("data");

if (adapters == null)
throw new ArgumentNullException("adapters");

Dictionary<DataTable, bool> procesedTables = new Dictionary<DataTable, bool>();
List<DataTable> sortedTables = new List<DataTable>();

while (true)
{
DataTable rootTable = GetRootTable(data, procesedTables);
if (rootTable == null)
break;

sortedTables.Add(rootTable);
}

/// Updating Deleted rows in Child -> Parent order.
for (int i = sortedTables.Count - 1; i >= 0; i--)
{
Update(adapters, sortedTables[i], DataViewRowState.Deleted);
}

/// Updating Added / Modified rows in Parent -> Child order.
for (int i = 0; i < sortedTables.Count; i++)
{
Update(adapters, sortedTables[i], DataViewRowState.Added | DataViewRowState.ModifiedCurrent);
}
}

static void Update(Dictionary<DataTable, SqlDataAdapter> adapters, DataTable table, DataViewRowState states)
{
SqlDataAdapter adapter = null;

if (adapters.ContainsKey(table))
adapter = adapters[table];

if (adapter != null)
{
DataRow[] rowsToUpdate = table.Select("", "", states);

if (rowsToUpdate.Length > 0)
adapter.Update(rowsToUpdate);
}
}

static DataTable GetRootTable(DataSet data, Dictionary<DataTable, bool> procesedTables)
{
foreach (DataTable table in data.Tables)
{
if (!procesedTables.ContainsKey(table))
{
if (IsRootTable(table, procesedTables))
{
procesedTables.Add(table, false);
return table;
}
}
}

return null;
}

static bool IsRootTable(DataTable table, Dictionary<DataTable, bool> procesedTables)
{
foreach (DataRelation relation in table.ParentRelations)
{
DataTable parentTable = relation.ParentTable;
if (parentTable != table && !procesedTables.ContainsKey(parentTable))
return false;
}

return true;
}

关于c# - 使用反射 (C#) 更新通用 DataTable 和 TableAdaptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1234797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com