OrE*_*lse 1 sql-server asp.net ado.net bulkinsert insert
我实施的当前解决方案非常糟糕!
我使用a for... loop将ADO.NET数据表中的记录插入到SQL表中.
我想立即将数据表插入到SQL表中,而不需要迭代...
这可能吗,还是我问得太多了?
您可以将整个DataTable作为单个表值参数传递,并立即插入整个TVP.以下是SQL Server 2008(ADO.NET)中的表值参数的示例:
// Assumes connection is an open SqlConnection.
using (connection)
{
// Create a DataTable with the modified rows.
DataTable addedCategories = CategoriesDataTable.GetChanges(
DataRowState.Added);
// Define the INSERT-SELECT statement.
string sqlInsert =
"INSERT INTO dbo.Categories (CategoryID, CategoryName)"
+ " SELECT nc.CategoryID, nc.CategoryName"
+ " FROM @tvpNewCategories AS nc;"
// Configure the command and parameter.
SqlCommand insertCommand = new SqlCommand(
sqlInsert, connection);
SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
"@tvpNewCategories", addedCategories);
tvpParam.SqlDbType = SqlDbType.Structured;
tvpParam.TypeName = "dbo.CategoryTableType";
// Execute the command.
insertCommand.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)
TVP仅适用于SQL 2008.
| 归档时间: |
|
| 查看次数: |
10988 次 |
| 最近记录: |