Bulk -Update in C#
Posted: 14 Apr 2025, 11:24
Um eine große Datenmenge in eine Datenbank einzuführen, habe ich alle Einfügeninformationen in eine Liste gesammelt und diese Liste in einen DataTable umgewandelt. Ich füge diese Liste dann über SQLBULKCOPY < /code> in eine Datenbank ein.LiMyList< /code>
, die Informationen zu allen Massendaten enthalten, die ich in die Datenbank einfügen möchte
und an meinen Masseneinfügungsvorgang weitergeben < /p>
InsertData(LiMyList, "MyTable");
< /code>
wobei InsertData < /code> < /p>
ist public static void InsertData(List list,string TableName)
{
DataTable dt = new DataTable("MyTable");
clsBulkOperation blk = new clsBulkOperation();
dt = ConvertToDataTable(list);
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["SchoolSoulDataEntitiesForReport"].ConnectionString))
{
bulkcopy.BulkCopyTimeout = 660;
bulkcopy.DestinationTableName = TableName;
bulkcopy.WriteToServer(dt);
}
}
public static DataTable ConvertToDataTable(IList data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
< /code>
Jetzt möchte ich eine Aktualisierungsoperation durchführen. Gibt es eine Möglichkeit, dass das Einfügen von Daten von SQLBULKCOPY < /code> zum Aktualisieren von Daten in Datenbank von C#.NET
< /P durchgeführt wird>
, die Informationen zu allen Massendaten enthalten, die ich in die Datenbank einfügen möchte
und an meinen Masseneinfügungsvorgang weitergeben < /p>
InsertData(LiMyList, "MyTable");
< /code>
wobei InsertData < /code> < /p>
ist public static void InsertData(List list,string TableName)
{
DataTable dt = new DataTable("MyTable");
clsBulkOperation blk = new clsBulkOperation();
dt = ConvertToDataTable(list);
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["SchoolSoulDataEntitiesForReport"].ConnectionString))
{
bulkcopy.BulkCopyTimeout = 660;
bulkcopy.DestinationTableName = TableName;
bulkcopy.WriteToServer(dt);
}
}
public static DataTable ConvertToDataTable(IList data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
< /code>
Jetzt möchte ich eine Aktualisierungsoperation durchführen. Gibt es eine Möglichkeit, dass das Einfügen von Daten von SQLBULKCOPY < /code> zum Aktualisieren von Daten in Datenbank von C#.NET
< /P durchgeführt wird>