Pass (Data Table) to SQL Server From ASP.NET using Enterprise Library

喜你入骨 提交于 2019-12-05 03:37:58

问题


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

I have a method like the above, here i am passing the datatable to the stored procedure by adding the parameter."DbType.Object" is not taking the datatable type. I know in ADO we can use "SqlDbType.Structured", but for enterprise libray it is not working. What i have to use instead?

I am getting the following error while executing the command

"The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 ("@candidateSkillSets"): Data type 0x62 (sql_variant) has an invalid type for type-specific metadata."


回答1:


I have to modify @eduardo's code to make me work in my case with 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);
            }
        }
    }

My type in SQL Server:

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

I hope help someone




回答2:


"DbType.Structured" Table valued parameters are not supported in the enterprise library . If you want to use a table as a parameter to a stored proc or query, you'll have to use the underlying store connection and the support that is provided there.




回答3:


DbType.Structured Table valued parameters are not supported in the enterprise library, but it is posible to used this way:

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



来源:https://stackoverflow.com/questions/30662943/pass-data-table-to-sql-server-from-asp-net-using-enterprise-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!