Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass List as Sql Table Type Parameter

I'm trying to pass the list of my class as 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.

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.

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

like image 219
Mayur Paghdal Avatar asked Jun 05 '14 13:06

Mayur Paghdal


People also ask

How do I pass a list as parameter in SQL stored procedure?

CREATE FUNCTION dbo. SplitInts ( @List VARCHAR(MAX), @Delimiter VARCHAR(255) ) RETURNS TABLE AS RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM ( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') + '</i>'). query('.

Can I pass a table as a parameter in SQL?

You can declare table-valued variables within dynamic Transact-SQL statements and pass these variables as table-valued parameters to stored procedures and functions.

How do you pass a table datatype to a procedure in SQL Server?

Create a user-defined table type that corresponds to the table that you want to populate. Pass the user-defined table to the stored procedure as a parameter. Inside the stored procedure, select the data from the passed parameter and insert it into the table that you want to populate.


1 Answers

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

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

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

very useful. :)

like image 120
Mayur Paghdal Avatar answered Sep 30 '22 03:09

Mayur Paghdal