c# - 将 DataTable 作为参数发送到存储过程

标签 c# stored-procedures .net-2.0 sql-server-2012-express

我正在尝试使用 c#、.net 2.0 和 SQLServer 2012 Express 将 DataTable 发送到存储过程。

这大致就是我正在做的:

        //define the DataTable
        var accountIdTable = new DataTable("[dbo].[TypeAccountIdTable]");

        //define the column
        var dataColumn = new DataColumn {ColumnName = "[ID]", DataType = typeof (Guid)};

        //add column to dataTable
        accountIdTable.Columns.Add(dataColumn);

        //feed it with the unique contact ids
        foreach (var uniqueId in uniqueIds)
        {
            accountIdTable.Rows.Add(uniqueId);
        }

        using (var sqlCmd = new SqlCommand())
        {
            //define command details
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "[dbo].[msp_Get_Many_Profiles]";
            sqlCmd.Connection = dbConn; //an open database connection

            //define parameter
            var sqlParam = new SqlParameter();
            sqlParam.ParameterName = "@tvp_account_id_list";
            sqlParam.SqlDbType = SqlDbType.Structured;
            sqlParam.Value = accountIdTable;

            //add parameter to command
            sqlCmd.Parameters.Add(sqlParam);

            //execute procedure
            rResult = sqlCmd.ExecuteReader();

            //print results
            while (rResult.Read())
            {
                PrintRowData(rResult);
            }
        }

但随后出现以下错误:

ArgumentOutOfRangeException: No mapping exists from SqlDbType Structured to a known DbType.
Parameter name: SqlDbType

经过进一步调查(在 MSDN、SO 和其他地方),似乎 .net 2.0 不支持向数据库发送 DataTable(缺少 SqlParameter.TypeName 等内容),但我我仍然不确定,因为我还没有看到任何人明确声称此功能在 .net 2.0 中不可用

这是真的吗?

如果是这样,是否有另一种方法可以将数据集合发送到数据库?

提前致谢!

最佳答案

开箱即用,ADO.NET 不支持它是有充分理由的。 DataTable 可以包含任意数量的列,这些列可能会或可能不会映射到数据库中的真实表。

如果我理解你想要做什么 - 将 DataTable 的内容快速上传到具有相同结构的预定义真实表,我建议你调查 SQLBulkCopy .

来自文档:

Microsoft SQL Server includes a popular command-prompt utility named bcp for moving data from one table to another, whether on a single server or between servers. The SqlBulkCopy class lets you write managed code solutions that provide similar functionality. There are other ways to load data into a SQL Server table (INSERT statements, for example), but SqlBulkCopy offers a significant performance advantage over them.

The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.

SqlBulkCopy will fail when bulk loading a DataTable column of type SqlDateTime into a SQL Server column whose type is one of the date/time types added in SQL Server 2008.

但是,您可以在更高版本的 SQL Server 中定义表值参数,并使用它以您要求的方法发送表 (DateTable)。在 http://sqlwithmanoj.wordpress.com/2012/09/10/passing-multipledynamic-values-to-stored-procedures-functions-part4-by-using-tvp/ 有一个例子

关于c# - 将 DataTable 作为参数发送到存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14112128/

相关文章:

c# - 从 C# 中的字符串数组获取随机值的最快方法?

c# - Unity动画播放两次

c# - 如何在统一的 XML 配置中将一个单例注册到不同的接口(interface)?

stored-procedures - 如何在 Hibernate 中创建临时表?

c# - 有没有办法从 ExcelWorkBook 中获取 byte[] 而无需先将其保存到磁盘

c# - 如何从数组中删除元素

c# - 为什么 10 亿 * 100 == 12 亿?

c# - 动态定义需要识别值的键名

mysql - 返回MySQL中的过程执行代码

MySQL:DEFINER 对过程和函数有什么影响?