简体   繁体   中英

Call method in class that passes parameter to a stored procedure in DAL method

I have two classes Program.cs and DataAccessLayer.cs .

I have a stored procedure that "lives" in the DataAccessLayer .

What I am trying to do is when the Program.cs runs, I need it to pass TWO parameters ( @Department1 and @Department2 ) to the stored procedure in the DAL.

Once I call the DAL method in the Program , I need to store the stored procedure results in a file. I understand that I will have to call the DAL method twice (once for Dept1, a second time for Dept2).

My question is: how do I format the Program method to pass the parameter for Dept1, and then store those results in an Excel file?

I'm assuming once I get the method in the Program class for Dept1, I can just copy and paste it for Dept2 and then just change the parameter (@Department2) that needs to be passed.

I am not worried about the @StartDate/@EndDate parameters since those don't change with each run of the stored procedure.

It should be noted that my variable (@DepartmentID) is a static variable in the stored procedure.

Basically what this stored procedure is designed to do is take the parameter of Dept1, return results that the app will put in a file.

Then I call the method in question again, this time with Dept2 Parameter, and return those results. Hope this made sense...

This is what I have in the DAL currently...

public bool GetDeptData(SqlConnection Conn, string StartDate,string EndDate, out DataTable DTDepartmentData)
{
    SqlDataAdapter adapter = null;
    SqlCommand cmd = null;

    DateTime startDate = new DateTime();
    DateTime endDate = new DateTime();

    SqlDbType DepartmentID = new SqlDbType();
    DTDepartmentData = new DataTable();

    try
    {
        // Set our start date and end date for last month
        startDate = DateTime.Parse(
            DateTime.Now.AddMonths(-1).Year.ToString() + "-" +
            DateTime.Now.AddMonths(-1).Month.ToString() + "-01 00:00:00");
        endDate = DateTime.Parse(
            DateTime.Now.AddMonths(-1).Year.ToString() + "-" +
            DateTime.Now.AddMonths(-1).Month.ToString() + "-" +
            DateTime.DaysInMonth(DateTime.Now.AddMonths(-1).Year,  DateTime.Now.AddMonths(-1).Month) + " 23:59:59");



        // Call our stored procedure and populate the datatable with the Dept data
        adapter = new SqlDataAdapter();
        cmd = new SqlCommand("sp_EXAMPLESP", Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startDate;
        cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endDate;
        cmd.Parameters.Add("@DepartmentID", SqlDbType.VarChar).Value = "departmentID"; 
        adapter.SelectCommand = cmd;
        adapter.Fill(DTDepartmentData);

        return true;
    }
    catch (Exception CaughtError)
    {
        this.ErrorMsg = "An error occurred in the GetDeptData method: " + CaughtError.ToString() + " || " + Environment.NewLine;
        return false;
    }

To pass the value of departmentID from main to your DAL method start changing the signature of the GetDeptData method to this one

public bool GetDeptData(string StartDate,string EndDate, string deptId, out DataTable DTDepartmentData)
{

     // The SqlConnection should be created here and destroyed 
     // here, everytime you call this method

     using(SqlConnection con = new SqlConnection(....connectionstring....))
     {
        // then use the deptId value in the creation 
        // of the parameter @DepartmentId
       .....
       cmd.Parameters.Add("@DepartmentID", SqlDbType.VarChar).Value = deptId; 
       adapter.Fill(DTDepartmentData);
       return true;

    }
 }
 catch(Exception ....)
 {
     ...
 }  

Now in your Program.cs you should create an instance of your DAL class and call the method passing the values required

 DataTable dtResult = null;
 string startDate = SomeCodeToGetTheStartDate();
 string endDate = SomeCodeToGetTheEndDate();
 string deptId = SomeCodeToGetTheDeptId();
 MyDALClass cs = new MyDALClass();
 if(cs.GetDeptData(startDate, endDate, deptId, out dtResult)
     .... success ....

Side note: Are you sure that the departmentId is a string value and not an integer?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM