简体   繁体   English

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

[英]Pass List as Sql Table Type Parameter

I'm trying to pass the list of my class as DbParameter .我正在尝试将我的班级列表作为DbParameter传递。 Probably the table type is defined in my stored procedure.可能表类型是在我的存储过程中定义的。

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<>传递到存储过程中,因为定义了只接受Tables的表类型。

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 List 1 to a IEnumerable 1.无法将参数值从 List 1 to a IEnumerable 1。

I know i can convert this list into DataTable and then pass it in the stored procedure but it seems time consuming.我知道我可以将此列表转换为DataTable ,然后将其传递到存储过程中,但这似乎很耗时。 :( :(

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>但是这篇文章对于通过object或者List<Obj>进行数据交易非常有用

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

very useful.很有用。 :) :)

I know this question a bit old, but I found another way to pass a list to table type parameter by converting list into datatable.我知道这个问题有点老了,但我找到了另一种通过将列表转换为数据表来将列表传递给表类型参数的方法。 So your code can be like this :所以你的代码可以是这样的:

public static AddCustomer(DataTable customer)
{
      try
      {
        DbCommand dbcomm = CDDAC.GetStoredProcCommand("name your stored procedure");
        CDDAC.AdInParameter(dbcomm, "@Param", SqlDbType.Structured, customer); //@param is an input parameter from your storedprocedure
        CDDAC.ExecuteNonQuery(dbcomm);
      }
      catch (Exception ex)
      {
        throw;
      }
    }

Just make sure you have created type for data customer in your database.只需确保您已在数据库中为数据客户创建类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM