locked
How to modify this function to accept Stored Procedure RRS feed

  • Question

  • Problem

    I need to modify this function to accept stored procedure in place of tsql statment

    Details

    I using function execute data table this function working only by passing tsql statement from c# 

    I need to modify this function to execute stored procedure with two parameters Year,MemberCode

    Function is ExecuteDatatable

        string SQL = @"Select * From View_MembersWithPriceList where  (MemberCode = N'" + txtMemberCode + "') and (RelatedMemberCode  IS NULL) and (Member ='True')  AND (Year = " + txtYear.Text + ")";
            DTPriceList = DataAccess.ExecuteDataTable(SQL);
    public static DataTable ExecuteDataTable(string sql, DbParameter[] dbprmParameters = null)
        {
            return ExecuteDataTable(sql, null, dbprmParameters);
        }
        public static DataTable ExecuteDataTable(string sql, IDbConnection dbConnection, DbParameter[] @params = null)
        {
            if (sql == "") return new DataTable();
            DataSet ds = new DataSet();
    
           
            lock (synObj)
            {
                
                sql = AnalyizeBooleanFields(sql);
                cmd.CommandText = sql;
                cmd.Parameters.Clear();
    
                if (@params != null)
                {
                    for (int i = 0; i < @params.Length; i++)
                    {
                        cmd.Parameters.Add(@params[i]);
                    }
                }
                if (dbConnection == null)
                {
                    if (WithTransaction)
                        dbConnection = BeginTransaction();
                    else
                        dbConnection = InitializeConnection();
                }
                if (dbConnection.State != ConnectionState.Open) dbConnection.Open();
                if (WithTransaction) cmd.Transaction = _transaction;
                cmd.Connection = dbConnection;
                IDbDataAdapter dbCurrentDataAdapter = InitializeDataAdapter();
                dbCurrentDataAdapter.SelectCommand = cmd;
                dbCurrentDataAdapter.Fill(ds);
    
                if (!WithTransaction) dbConnection.Close();
            }
    
            return ds.Tables[0];
            
        }

    Actually in function ExecuteDatatable(sql);

    I need to modify this function to accept stored procedure and two parameters for

    txtYear,
    txtMemberCode

    So that FunctionAfter modify i need calling to be

    ExecuteDatatable(ShowViewMembers,@Year,@Membercode)

    Create proc ShowViewMembers

    @Membercode int ,

    @Year   int

    as

    Select * From View_MembersWithPriceList where MemberCode=@Membercode and Year=@Year 


    Friday, June 22, 2018 2:24 PM

Answers

  • Hi,

    >>I need to modify this function to accept stored procedure and two parameters ...

    If so, you also need to pass a parameter to define the command type:

            public static DataTable ExecuteDataTable(string sql, IDbConnection dbConnection, DbParameter[] @params = null, CommandType commandType)
            {
                if (sql == "") return new DataTable();
                DataSet ds = new DataSet();
    
                lock (synObj)
                {
                    sql = AnalyizeBooleanFields(sql);
                    cmd.CommandText = sql;
                    cmd.Parameters.Clear();
                    cmd.CommandType = commandType;
    
                    if (@params != null)
                    //...
                }
    
                return ds.Tables[0];
            }

    Then called it:

        public static DataTable ExecuteDataTable(string sql, DbParameter[] dbprmParameters = null)
        {
            return ExecuteDataTable(sql, null, dbprmParameters, CommandType.StoredProcedure);
        }

    Also please refer to the following documents about stored procedures in ADO.NET:

    Calling Stored procedures in ADO.NET

    HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by BonnieBMVP Tuesday, June 26, 2018 1:10 AM
    • Marked as answer by engahmedbarbary Thursday, June 28, 2018 12:54 AM
    Monday, June 25, 2018 6:40 AM