【问题标题】:Pass (Data Table) to SQL Server From ASP.NET using Enterprise Library使用企业库从 ASP.NET 将(数据表)传递到 SQL Server
【发布时间】:2015-06-05 09:25:52
【问题描述】:
     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) 对于特定于类型的类型无效 元数据。”

【问题讨论】:

    标签: c# asp.net enterprise-library


    【解决方案1】:

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

    希望能帮到人

    【讨论】:

      【解决方案2】:

      “DbType.Structured” 企业库不支持表值参数。如果您想将表用作存储过程或查询的参数,则必须使用底层存储连接和那里提供的支持。

      【讨论】:

      • 您也可以在企业库中使用 SqlDbType.Structured
      • 企业库中没有 DbType.Structured 的替代品
      • 我尝试使用SqlDbType.Structured。但没有用。您能否在Enterprise Library 中添加一些使用SqlDbType.Structured 的代码
      【解决方案3】:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-10
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多