Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

c# - How to Pass List<> to stored procedure

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...