2013-01-01 38 views
4

我想发送一个DataTable到存储过程使用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

可用

这是真的吗?

如果是这样,有另一种方式来发送数据的集合到数据库?

在此先感谢!

+0

我相信,在SQL Server 2008中添加表值参数,所以他们很可能无法在.NET 2.0 。您可以发送XML,并将其从存储过程解析为表变量? –

+0

@lc。 - 我可以,但它远非最佳。 :( –

+0

同意它远离最佳状态,我只是试图在盒子外面思考一下,在你的情况中,你似乎想要发送一串GUID,并且因为它们是固定的大小,你甚至可能会能够在varbinary中将它们端到端地排列起来,然后在过程的循环中将它们拆分回表变量中。对于更复杂的东西不是最佳的,但它可能在这里起作用 –

回答

1

开箱,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/

+0

我创建了类型,但我似乎无法从c#发送DataTable。 –

+0

那么,我设法在数据库方面变得更聪明,并实现了我想要的效果,而无需发送DataTable。 显然没有办法与.net 2,这是一种耻辱,但这就是生活。无论如何感谢您的帮助! –

0

每我的经验为例,如果你能够编译C#代码,这意味着该ADO.Net支持该类型。但是,如果执行代码时失败,那么目标数据库可能不支持它。在你的情况下,你提到[Sql Server 2012 Express],所以它可能不支持它。根据我的理解,Table Type受到[Sql Server 2005]的支持,但您必须将数据库兼容模式保持为大于99或更高。我是100%的,将在2008年工作,因为我已经用它和使用它广泛地做,通过存储过程批量更新使用[用户定义的表类型(a.k.a UDTT)作为在参数存储过程。再次,您必须保持数据库兼容性大于99以使用MERGE命令进行批量更新。

当然,你可以使用SqlBulkCopy的,但不知道它是如何可靠,是取决于

相关问题