SQL Server 2012 Insert Stored Procedure for N parameters

Muhammad Zeeshan

I have a table with 70 columns in SQL Server 2012. I am using 3-Tier Architecture in ASP.NET. How can I store the values in that table with minimal code.

Following are the properties which are to be stored in table.

 public int DeptID { get; set; }
    public int ProgID { get; set; }

    public string Session { get; set; }
    public string Name { get; set; }
    public string FName { get; set; }
    public string FOccupation { get; set; }
    public string FIncome { get; set; }
    public string FNIC { get; set; }
    public string ANIC { get; set; }
    public string DBirth { get; set; }
    public string Gender { get; set; }
    public string Religion { get; set; }
    public string Martial_Status { get; set;}
    public string Nationality { get; set; }
    public string Domicile { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string PAddress { get; set; }
    public string MAddress { get; set; }
    public string Service { get; set; }
    public string Designation { get; set; }
    public string Organization { get; set; }

    public int SSCYear { get; set; }
    public string SSCEBody { get; set; }
    public int SSCRNo { get; set; }
    public int SSCTMarks { get; set; }
    public  int SSCOMarks { get; set; }
    public string SSCDivision { get; set; }
    public string SSCMSubjects { get; set; }

    public int IntermidiateYear { get; set; }
    public string IntermidiateEBody { get; set; }
    public int IntermidiateRNo { get; set; }
    public int IntermidiateTMarks { get; set; }
    public int IntermidiateOMarks { get; set; }
    public string IntermidiateDivision { get; set; }
    public string IntermidiateMSubjects { get; set; }

    public int UGraduate1Year { get; set; }
    public string UGraduate1EBody { get; set; }
    public int UGraduate1RNo { get; set; }
    public int UGraduate1TMarks { get; set; }
    public int UGraduate1OMarks { get; set; }
    public string UGraduate1Division { get; set; }
    public string UGraduate1MSubjects { get; set; }

    public int UGraduate2Year { get; set; }
    public string UGraduate2EBody { get; set; }
    public int UGraduate2RNo { get; set; }
    public int UGraduate2TMarks { get; set; }
    public int UGraduate2OMarks { get; set; }
    public string UGraduate2Division { get; set; }
    public string UGraduate2MSubjects { get; set; }

    public int GraduateYear { get; set; }
    public string GraduateEBody { get; set; }
    public int GraduateRNo { get; set; }
    public int GraduateTMarks { get; set; }
    public int GraduateOMarks { get; set; }
    public string GraduateDivision { get; set; }
    public string GraduateMSubjects { get; set; }

    public int GatGeneralRNo { get; set; }
    public int GatGeneralScore { get; set; }
    public string GatGeneralFStudy { get; set; }
    public string GatGeneralTestDate { get; set; }
    public string GatGeneralValid { get; set; }

    public int GatSubjectRNo { get; set; }
    public int GatSubjectScore { get; set; }
    public string GatSubjectFStudy { get; set; }
    public string GatSubjectTestDate { get; set; }
    public string GatSubjectValid { get; set; }

Here is the fuction for calling the procedure

 public DataTable GraduateProgInsert()
    {

        SqlParameter[] prm = new SqlParameter[72];

        //how to add these values in the SqlParameter

    }
Ruben Steins

You could use reflection to go over all the properties and store them in the array:

foreach(var property in myClass.GetType().GetProperties())
{
    SqlParameter newParam = new SqlParameter();
    newParam.Name = property.Name;
    ...
    // set other fields of the new parameter here and add it to the array 
    // the logic to determine the exact type of param can get hairy
 }

Using an ORM is another alternative I would certainly consider to avoid having to write this kind of code yourself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rollback in single stored procedure for forloop & insert query in SQL Server 2012

From Dev

Stored procedure in SQL Server 2012

From Dev

SQL Server 2012 - Pass stored procedure name to another stored procedure

From Dev

Using insert stored procedure with SQL Server

From Dev

Insert into @tablename in SQL Server stored procedure

From Dev

SQL Server stored procedure to insert in multiple tables

From Dev

SQL Server Stored Procedure to insert selected data

From Dev

SQL Server stored procedure to insert in multiple tables

From Dev

SQL Server : Insert cursor within stored procedure

From Dev

SQL Server insert table column to stored procedure

From Dev

Stored Procedure with Optional DateTime Parameters in SQL Server

From Dev

Optional parameters in stored procedure in SQL Server

From Dev

SQL Server Stored Procedure append parameters format

From Dev

ASP parameters to call a stored procedure in SQL Server

From Dev

Executing stored procedure with parameters SQL Server 2005

From Dev

SQL Server stored procedure for Insert runs, but does not insert values into the table

From Dev

Execute multiple statements and CTE in stored procedure in SQL Server 2012?

From Dev

How to give table name dynamically in stored procedure in SQL Server 2012

From Dev

How to create a select, and then an update stored procedure in SQL Server 2012

From Dev

tsql not firing (stored procedure via ssis) - SQL Server 2012

From Dev

How to give table name dynamically in stored procedure in SQL Server 2012

From Dev

Execute Stored Procedure with RollbackTrans using VBA and SQL Server 2012

From Dev

SQL Server stored procedure with 2 input parameters and 8 output parameters

From Dev

Insert Stored Procedure with null parameters

From Dev

T-SQL Stored procedure insert if exists with out parameters error

From Dev

SQL Server 2012 Write Return Values of Stored Procedure to Cells using Stored Procedure

From Dev

SQL server : stored procedure

From Dev

SQL Server stored procedure display message after insert

From Dev

SQL Server stored procedure - check if ANY records exist, and insert if not

Related Related

  1. 1

    Rollback in single stored procedure for forloop & insert query in SQL Server 2012

  2. 2

    Stored procedure in SQL Server 2012

  3. 3

    SQL Server 2012 - Pass stored procedure name to another stored procedure

  4. 4

    Using insert stored procedure with SQL Server

  5. 5

    Insert into @tablename in SQL Server stored procedure

  6. 6

    SQL Server stored procedure to insert in multiple tables

  7. 7

    SQL Server Stored Procedure to insert selected data

  8. 8

    SQL Server stored procedure to insert in multiple tables

  9. 9

    SQL Server : Insert cursor within stored procedure

  10. 10

    SQL Server insert table column to stored procedure

  11. 11

    Stored Procedure with Optional DateTime Parameters in SQL Server

  12. 12

    Optional parameters in stored procedure in SQL Server

  13. 13

    SQL Server Stored Procedure append parameters format

  14. 14

    ASP parameters to call a stored procedure in SQL Server

  15. 15

    Executing stored procedure with parameters SQL Server 2005

  16. 16

    SQL Server stored procedure for Insert runs, but does not insert values into the table

  17. 17

    Execute multiple statements and CTE in stored procedure in SQL Server 2012?

  18. 18

    How to give table name dynamically in stored procedure in SQL Server 2012

  19. 19

    How to create a select, and then an update stored procedure in SQL Server 2012

  20. 20

    tsql not firing (stored procedure via ssis) - SQL Server 2012

  21. 21

    How to give table name dynamically in stored procedure in SQL Server 2012

  22. 22

    Execute Stored Procedure with RollbackTrans using VBA and SQL Server 2012

  23. 23

    SQL Server stored procedure with 2 input parameters and 8 output parameters

  24. 24

    Insert Stored Procedure with null parameters

  25. 25

    T-SQL Stored procedure insert if exists with out parameters error

  26. 26

    SQL Server 2012 Write Return Values of Stored Procedure to Cells using Stored Procedure

  27. 27

    SQL server : stored procedure

  28. 28

    SQL Server stored procedure display message after insert

  29. 29

    SQL Server stored procedure - check if ANY records exist, and insert if not

HotTag

Archive