将列表作为Sql表类型参数传递

时间:2021-12-16 10:26:41

I'm trying to pass the list of my class as DbParameter. Probably the table type is defined in my stored procedure.

我正在尝试将我的班级列表作为DbParameter传递。表格类型可能在我的存储过程中定义。

Now, I'm not getting how to pass the List<> into the stored procedure as there is table type defined which accepts Tables only.

现在,我没有得到如何将List <>传递给存储过程,因为定义的表类型只接受表。

Here, I'm putting my method.

在这里,我正在采用我的方法。

public static AddCustomer(List<Customer> customer)
{
      List<DbParameter> lstDbParameters = null;

      try
      {
        #region Set the Parameters
        lstDbParameters = new List<DbParameter>();
        SqlParameter dbAcceptedBillDetails = new SqlParameter("@Customers",
                                                             customer);

        dbAcceptedBillDetails.SqlDbType = SqlDbType.Structured;
        lstDbParameters.Add(dbAcceptedBillDetails as DbParameter);
        lstDbParameters.Add(CDDAC.MakeDbParameter(dbProvider,
                                                  "@ErrorMessage",
                                                  DbType.String,
                                                  null,
                                                  500,
                                                  ParameterDirection.Output));
        #endregion

        //Call the static ExecuteNonQuery method.
        CDDAC.ExecuteNonQuery(dbProvider,
                              connectionString,
                              "AddCustomer",
                              CommandType.StoredProcedure,
                              lstDbParameters.ToArray());
      }
      catch (Exception ex)
      {
        throw;
      }
    }

And I'm getting error like this:

我收到这样的错误:

Failed to convert parameter value from a List1 to a IEnumerable1.

无法将参数值从List1转换为IEnumerable1。

I know i can convert this list into DataTable and then pass it in the stored procedure but it seems time consuming. :(

我知道我可以将此列表转换为DataTable,然后在存储过程中传递它,但这似乎很耗时。 :(

1 个解决方案

#1


7  

Finally, i got my answer byself. But during finding, i got that there is no way exist to convert List<> to IEnumerable<> directly.

最后,我自己得到了答案。但在查找过程中,我发现没有办法直接将List <>转换为IEnumerable <>。

But this article is very useful to transact data through object or List<Obj>

但是这篇文章对于通过对象或List 进行数据交换非常有用

http://www.c-sharpcorner.com/UploadFile/pchandraker/passing-table-valued-parameter-to-stored-procedu-part-2/

very useful. :)

很有用。 :)

#1


7  

Finally, i got my answer byself. But during finding, i got that there is no way exist to convert List<> to IEnumerable<> directly.

最后,我自己得到了答案。但在查找过程中,我发现没有办法直接将List <>转换为IEnumerable <>。

But this article is very useful to transact data through object or List<Obj>

但是这篇文章对于通过对象或List 进行数据交换非常有用

http://www.c-sharpcorner.com/UploadFile/pchandraker/passing-table-valued-parameter-to-stored-procedu-part-2/

very useful. :)

很有用。 :)