将数据表传递到存储过程。有没有更好的办法?

将数据表传递到存储过程。有没有更好的办法?,第1张

数据表传递到存储过程。有没有更好的办法?

您可以在SQL中创建用户定义的表类型。然后,在存储过程中,接受类型(您用户定义的表类型)的参数,并将数据表作为其值传递给存储过程。

这是来自http://msdn.microsoft.com/zh-
cn/library/bb675163.aspx
的一些示例:

在SQL中:

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();}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5666386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存