How to Pass List<> to stored procedure

允我心安 提交于 2019-12-13 10:14:07

问题


I am trying to pass a list to database (SQL Server 2008 R2) using user defined table types but get some errors my code is:

public int DAL_SaveIncramentSalary(tbl_Employee_Master obj, List < dtIncrementSalary > tbl) {
    try {
        SqlParameter[] objSqlParameter = new SqlParameter[4];
        objSqlParameter[0] = new SqlParameter("@Company_ID", obj.Company_ID);
        objSqlParameter[1] = new SqlParameter("@Employee_ID", obj.Employee_ID);
        objSqlParameter[2] = new SqlParameter("@Salary_Month", obj.Govt_DA);
        objSqlParameter[3] = new SqlParameter("@dt", SqlDbType.Structured);
        objSqlParameter[3].Value = tbl;
        objSqlParameter[3].Direction = ParameterDirection.Input;
        DataSet fdd = SqlHelper.ExecuteDataset(_cnnString2, "usp_Insert_Increment_Salary_List", CommandType.StoredProcedure, bjSqlParameter);
        DataSet fddd = SqlHelper.ExecuteDataset(_cnnString2, "usp_Insert_Increment_Salary_List", objSqlParameter);
        DataSet ds = SqlHelper.ExecuteDataset(_cnnString2, CommandType.StoredProcedure, "usp_Insert_Increment_Salary_List", objSqlParameter);
        return SqlHelper.ExecuteNonQuery(_cnnString2, CommandType.StoredProcedure, "usp_Insert_Increment_Salary_List", objSqlParameter);
    } catch (Exception ex) {
        throw new Exception(ex.Message);
    }
}

I got error like this:

Dataset dff error : Parameter count does not match Parameter Value count. DataSet fddd error : ailed to convert parameter value from a List1 to a IEnumerable1. DataSet ds error :Failed to convert parameter value from a List1 to a IEnumerable1.

SQL Server user defined type

CREATE type dtincrementsalary AS TABLE
(
    head_id INT NULL, 
    salamt NUMERIC(18,2) NULL, 
    per FLOAT NULL, 
    oldsalamt NUMERIC(18,2) NULL,  
    oldper FLOAT NULL 
) sql PROCALTER

PROCEDURE [DBO].[Usp_insert_increment_salary_list] ( @Company_ID  INT, 
                                                    @Employee_ID  INT, 
                                                    @Salary_Month INT, 
                                                    @dt DTINCREMENTSALARY readonly ) 
AS 
BEGIN 
  SELECT * 
  FROM   @dt 
END

回答1:


Issue in your code objSqlParameter[3].Value = tbl;

tbl is List<dtIncrementSalary> but SqlDbType.Structured will take Table

suggest you to convert your List<dtIncrementSalary> in Datatable with same schema as you created in Sql Server(dtincrementsalary)



来源:https://stackoverflow.com/questions/35651097/how-to-pass-list-to-stored-procedure

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