当 table 中存在依赖项时,我该如何执行 SqlBulkCopy?
How can I do SqlBulkCopy when there is a dependency in the table?
当不依赖 table 时,我可以使用以下代码执行 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);
}
}
}
}
但是,当有依赖的时候,我就糊涂了。
1)我应该为每个 table 创建一个 DataTable 吗?
2) 由于 table 关系信息是由用户提供并加载到内存中的,我如何在不从模式中读取信息的情况下绑定 tables?或者我是否必须阅读信息并从架构中获取主键?
提前致谢。希望我说清楚了。
在这种情况下,唯一可以做到的方法是在没有键的情况下对临时表进行批量复制操作 linking 这两个对象(以及足够的元数据来构建 link稍后)。
假设您有 3 个表:#item_unit_staging
、#shop_order_staging
和 #shop_order_operation_staging
。您将使用与 item_unit
、shop_order
和 shop_order_operation
相同的模式创建它们,并使用自动生成的主键。您还希望将您用于 link 本地计算机内存中数据的任何数据添加为 "metadata column",假设我们在所有三个表上使用了一个名为 metadata_id
的额外列,并且#shop_order_staging
和 #shop_order_operation_staging
.
上名为 parent_id
的专栏
然后我们批量插入我们的 3 个表,填充我们的主键列并留下我们的外键列 NULL
。一旦我们在服务器上有了数据,我们就使用元数据列来填充外键列。
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
当不依赖 table 时,我可以使用以下代码执行 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);
}
}
}
}
但是,当有依赖的时候,我就糊涂了。
1)我应该为每个 table 创建一个 DataTable 吗?
2) 由于 table 关系信息是由用户提供并加载到内存中的,我如何在不从模式中读取信息的情况下绑定 tables?或者我是否必须阅读信息并从架构中获取主键?
提前致谢。希望我说清楚了。
在这种情况下,唯一可以做到的方法是在没有键的情况下对临时表进行批量复制操作 linking 这两个对象(以及足够的元数据来构建 link稍后)。
假设您有 3 个表:#item_unit_staging
、#shop_order_staging
和 #shop_order_operation_staging
。您将使用与 item_unit
、shop_order
和 shop_order_operation
相同的模式创建它们,并使用自动生成的主键。您还希望将您用于 link 本地计算机内存中数据的任何数据添加为 "metadata column",假设我们在所有三个表上使用了一个名为 metadata_id
的额外列,并且#shop_order_staging
和 #shop_order_operation_staging
.
parent_id
的专栏
然后我们批量插入我们的 3 个表,填充我们的主键列并留下我们的外键列 NULL
。一旦我们在服务器上有了数据,我们就使用元数据列来填充外键列。
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