使用Enterprise Library从ASP.NET传递(数据表)到SQL Server

public void updateSkills(DataTable candidateSkillSets) { string sqlCommand = "Sp_Candidate"; DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand); db.AddInParameter(dbCommand, "candidateSkillSets",DbType.Object, candidateSkillSets); db.ExecuteNonQuery(dbCommand); } 

我有一个像上面这样的方法,这里我通过添加参数将数据表传递给存储过程。“DbType.Object”没有采用数据表类型。 我知道在ADO中我们可以使用“SqlDbType.Structured”,但是对于企业库,它不起作用。 我必须使用什么?

执行命令时出现以下错误

“传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。参数1(”@candidateSkillSets“):数据类型0x62(sql_variant)具有类型特定元数据的无效类型。”

我必须修改@ eduardo的代码,让我在Enterprise Library中工作:

  public int Insert(int id, DTO mnemonics) { DatabaseProviderFactory factory = new DatabaseProviderFactory(); Database Db = factory.CreateDefault(); using (var dbCommand = Db.GetStoredProcCommand("spINSERT")) { using (var table = new DataTable()) { table.Columns.Add("Key", typeof(string)); table.Columns.Add("Value", typeof(string)); foreach (KeyValuePair item in mnemonics.data) { var row = table.NewRow(); row["Key"] = item.Key; row["Value"] = item.Value; table.Rows.Add(row); } Db.AddInParameter(dbCommand, "id", DbType.int, id); SqlParameter p = new SqlParameter("MNEMONICS", table); p.SqlDbType = SqlDbType.Structured; dbCommand.Parameters.Add(p); Db.ExecuteNonQuery(dbCommand); } } } 

我在SQL Server中的类型:

 CREATE TYPE [dbo].[StringDict] AS TABLE( [Key] [varchar](max) NULL, [Value] [varchar](max) NULL ) 

我希望能帮助别人

“DbType.Structured”企业库中不支持表值参数。 如果要将表用作存储过程或查询的参数,则必须使用基础存储连接和其中提供的支持。

企业库中不支持DbType.Structured Table值参数,但可以这样使用:

 db = DatabaseFactory.CreateDatabase() Using cmd As DbCommand = db.GetStoredProcCommand("Sp_Candidate") db.AddInParameter(cmd, "@candidateid", DbType.Int32, txtCandidateID.text) cmd.Parameters.Add(New SqlParameter("candidateSkillSets", candidateSkillSets) With {.SqlDbType = SqlDbType.Structured}) db.ExecuteNonQuery(cmd) End Using 

如何将数组传递给SQL Server存储过程