数据表格是否可以以某种方式传递到SQL Server 2005或2008中?
我知道标准方法似乎是将XML传递给存储过程。并且可以轻松将数据表转换为XML以执行此操作。
那么将.NET对象传递到存储过程中呢?这可能吗?
我记得在2008年听说过SQL和CLR在某种程度上可以一起使用,但我从未理解过.或许这意味着您可以在存储过程中引用.NET对象?
数据表格是否可以以某种方式传递到SQL Server 2005或2008中?
我知道标准方法似乎是将XML传递给存储过程。并且可以轻松将数据表转换为XML以执行此操作。
那么将.NET对象传递到存储过程中呢?这可能吗?
我记得在2008年听说过SQL和CLR在某种程度上可以一起使用,但我从未理解过.或许这意味着您可以在存储过程中引用.NET对象?
CREATE TYPE dbo.CategoryTableType AS TABLE
( CategoryID int, CategoryName nvarchar(50) )
然后:
// Assumes connection is an open SqlConnection object.
using (connection)
{
// Create a DataTable with the modified rows.
DataTable addedCategories =
CategoriesDataTable.GetChanges(DataRowState.Added);
// Configure the SqlCommand and SqlParameter.
SqlCommand insertCommand = new SqlCommand(
"usp_InsertCategories", connection);
insertCommand.CommandType = CommandType.StoredProcedure;
SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
"@tvpNewCategories", addedCategories);
tvpParam.SqlDbType = SqlDbType.Structured;
// Execute the command.
insertCommand.ExecuteNonQuery();
}
否则您可以选择 SQL 批量插入。
我尝试过了,是最好的方法。
enter code here
Using dbConn As New SqlConnection(connectionStr)
dbConn.Open()
countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(dbConn)
bulkcopy.DestinationTableName = "[dbo].[DocumentScan]"
Try
' Write from the source to the destination.
bulkcopy.WriteToServer(newDataTable)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Dim countEnd As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
End Using