使用Enterprise Library

时间:2015-06-05 09:25:52

标签: c# asp.net enterprise-library

     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)具有类型特定的无效类型   。元数据"

3 个答案:

答案 0 :(得分:10)

我必须修改@ 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<string, string> 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
)

我希望能帮助别人

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

db = DatabaseFactory.CreateDatabase();
using (DbCommand cmd = db.GetStoredProcCommand("Sp_Candidate"))
{
    db.AddInParameter(cmd, "@candidateid", DbType.Int32, txtCandidateID.text);
    cmd.Parameters.Add(new SqlParameter("candidateSkillSets", candidateSkillSets) { SqlDbType = SqlDbType.Structured });
    db.ExecuteNonQuery(cmd);
}

How to pass an array into a SQL Server stored procedure