C# 当表中存在依赖项时,如何执行SqlBulkCopy?

C# 当表中存在依赖项时,如何执行SqlBulkCopy?,c#,sqlbulkcopy,C#,Sqlbulkcopy,我可以使用以下代码在不依赖于表的情况下执行SqlBulkCopy: //TableData and FieldData have column info in memory. using (SqlConnection connection = new SqlConnection(@"Data Source=WLO1;Initial Catalog=GenTest2;Integrated Security=True")) { connection.Op

我可以使用以下代码在不依赖于表的情况下执行SqlBulkCopy:

//TableData and FieldData have column info in memory.  
using (SqlConnection connection = new SqlConnection(@"Data Source=WLO1;Initial Catalog=GenTest2;Integrated Security=True"))
        {

            connection.Open();
            foreach (string tableName in tablesInOrderToProcess)
            {
                if (tableDataList.ContainsKey(tableName))
                {
                    TableData td = tableDataList[tableName];

                    SqlBulkCopy copy = new SqlBulkCopy(connection);
                    copy.BatchSize = 10000;
                    copy.DestinationTableName = tableName;

                    System.Data.DataTable dt = new DataTable();
                    foreach (FieldData fd in td.FieldList.Values)
                    {
                        string fieldName = fd.Name;
                        string fieldDataType = fd.DataType;
                        string fieldSize = fd.Size.ToString(); ;
                        string fieldConstant = fd.constantValue;
                        string fieldAverage = fd.averageSize;
                        string fieldPickList = fd.pickList;
                        copy.ColumnMappings.Add(fieldName, fieldName);
                        switch (fieldDataType)
                        {
                            case "char":
                                {
                                    dt.Columns.Add(fieldName, System.Type.GetType("System.String"));
                                }
                                break;
                            case "nvarchar":
                                {
                                    dt.Columns.Add(fieldName, System.Type.GetType("System.String"));
                                }
                                break;
                            case "number":
                                {
                                    if (fd.Size == 10)
                                        dt.Columns.Add(fieldName, System.Type.GetType("System.Int64"));
                                    else
                                        dt.Columns.Add(fieldName, System.Type.GetType("System.Int32"));
                                }
                                break;
                            default:
                                {
                                    dt.Columns.Add(fieldName);
                                }
                                break;
                        }
                    }
                    for (int i = 0; i < int.Parse(td.NumRows); i++)
                    {
                        System.Data.DataRow r = dt.NewRow();
                        foreach (FieldData fd in td.FieldList.Values)
                        {
                            string fieldName = fd.Name;
                            string fieldDataType = fd.DataType;
                            string fieldSize = fd.Size.ToString(); ;
                            string fieldConstant = fd.constantValue;
                            string fieldAverage = fd.averageSize;
                            string fieldPickList = fd.pickList;

                            switch (fieldDataType)
                            {
                                case "char":
                                    {
                                        if (fd.averageSize.Length > 0)
                                        {
                                            r[fieldName] = GetRandomString(Int32.Parse(fd.averageSize), true);
                                        }
                                        else
                                        {
                                            r[fieldName] = fieldConstant;
                                        }
                                    }
                                    break;
                                case "nvarchar":
                                    {
                                        if (fd.averageSize.Length > 0)
                                        {
                                            r[fieldName] = GetRandomString(Int32.Parse(fd.averageSize), true);
                                        }
                                        else
                                        {
                                            r[fieldName] = fieldConstant;
                                        }
                                    }
                                    break;
                                case "number":
                                    {
                                        if (fd.Size == 10)
                                        {
                                            r[fieldName] = i;
                                        }
                                        else
                                        {
                                            r[fieldName] = i;
                                        }
                                    }
                                    break;
                                default:
                                    {
                                        r[fieldName] = fieldConstant;
                                    }
                                    break;
                            }

                        }
                        dt.Rows.Add(r);
                    }

                    try
                    {
                        copy.WriteToServer(dt);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
//TableData和FieldData在内存中有列信息。
使用(SqlConnection连接=新的SqlConnection(@“数据源=WLO1;初始目录=GenTest2;集成安全性=True”))
{
connection.Open();
foreach(tablesInOrderToProcess中的字符串tableName)
{
if(tableDataList.ContainsKey(tableName))
{
TableData td=tableDataList[tableName];
SqlBulkCopy=newsqlbulkcopy(连接);
copy.BatchSize=10000;
copy.DestinationTableName=表名;
System.Data.DataTable dt=新数据表();
foreach(td.FieldList.Values中的FieldData fd)
{
字符串fieldName=fd.Name;
字符串fieldDataType=fd.DataType;
字符串字段大小=fd.Size.ToString();
字符串字段常量=fd.constantValue;
字符串fieldAverage=fd.averageSize;
字符串字段pickList=fd.pickList;
copy.ColumnMappings.Add(fieldName,fieldName);
交换机(fieldDataType)
{
案例“char”:
{
Add(fieldName,System.Type.GetType(“System.String”);
}
打破
案例“nvarchar”:
{
Add(fieldName,System.Type.GetType(“System.String”);
}
打破
案例“编号”:
{
如果(fd.Size==10)
Add(fieldName,System.Type.GetType(“System.Int64”);
其他的
Add(fieldName,System.Type.GetType(“System.Int32”);
}
打破
违约:
{
dt.Columns.Add(字段名);
}
打破
}
}
for(int i=0;i0)
{
r[fieldName]=GetRandomString(Int32.Parse(fd.averageSize),true);
}
其他的
{
r[fieldName]=字段常量;
}
}
打破
案例“nvarchar”:
{
如果(fd.averageSize.Length>0)
{
r[fieldName]=GetRandomString(Int32.Parse(fd.averageSize),true);
}
其他的
{
r[fieldName]=字段常量;
}
}
打破
案例“编号”:
{
如果(fd.Size==10)
{
r[fieldName]=i;
}
其他的
{
r[fieldName]=i;
}
}
打破
违约:
{
r[fieldName]=字段常量;
}
打破
}
}
dt.行。加(r);
}
尝试
{
copy.WriteToServer(dt);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}
}
然而,当存在依赖时,我感到困惑。

1) 我应该为每个表创建一个DataTable吗

2) 自表关系信息
update #shop_order_staging 
    set parent_item_unit_id = #item_unit_staging.item_unit_id
    from #shop_order_staging
    inner join #item_unit_staging on #shop_order_staging.parent_id = #item_unit_staging.metadata_id

update #shop_order_operation_staging 
    set parent_shop_order_id = #shop_order_staging.shop_order_id
    from #shop_order_operation_staging
    inner join #shop_order_staging on #shop_order_operation_staging.parent_id = #shop_order_staging.metadata_id
insert into item_unit 
    select item_unit_id
         , /*all other columns except [metadata_id]*/ 
    from #item_unit_staging


insert into shop_order 
    select shop_order_id
         , parent_item_unit_id
         , /*all other columns except metadata_id and parent_id*/ 
    from #shop_order_staging

insert into shop_order_operation 
    select shop_order_operation_id
         , parent_shop_order_id
         , /*all other columns except metadata_id and parent_id*/ 
    from #shop_order_operation_staging