Search This Blog

Saturday, December 4, 2010

Creating Data Access Layer C# (Deborah's link)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccessLayer
{
    public class DAL
    {
        public static DataTable ExecuteDataTable(string storedProcedureName, params SqlParameter[] arrParam)
        {
            DataTable dt = new DataTable();

            // Open the connection
            using (SqlConnection cnn = new SqlConnection("Data Source=Provider=SQLOLEDB.1; Integrated Security=SSPI; Persist Security Info=False; User ID=EPU; Initial Catalog=EasyPay; Data Source=Home-PC\\SQLEXPRESS"))
            {
                cnn.Open();

                // Define the command
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storedProcedureName;

                    // Handle the parameters
                    if (arrParam != null)
                    {
                        foreach (SqlParameter param in arrParam)
                            cmd.Parameters.Add(param);
                    }

                    // Define the data adapter and fill the dataset
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                }
            }
            return dt;
        }

        public static int ExecuteNonQuery(string storedProcedureName, params SqlParameter[] arrParam)
            {
            int retVal=0;
            SqlParameter firstOutputParameter = null;

            // Open the connection
            using (SqlConnection cnn = new SqlConnection("Data Source=Provider=SQLOLEDB.1; Integrated Security=SSPI; Persist Security Info=False; User ID=EPU; Initial Catalog=EasyPay; Data Source=Home-PC\\SQLEXPRESS"))
            {
                cnn.Open();

                // Define the command
                using (SqlCommand cmd= new SqlCommand())
                {
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = storedProcedureName;

                    // Handle the parameters
                    if (arrParam != null)
                    {
                        foreach (SqlParameter param in arrParam)
                        {
                            cmd.Parameters.Add(param);
                            if (firstOutputParameter == null &&
                                param.Direction==ParameterDirection.Output &&
                                param.SqlDbType == SqlDbType.Int)
                                firstOutputParameter = param;
                        }
                    }

                    // Execute the stored procedure
                    cmd.ExecuteNonQuery();

                    // Return the first output parameter value
                    if (firstOutputParameter != null)
                        retVal = (int)firstOutputParameter.Value;
                }
            }
            return retVal;
            }
        }
    }

No comments:

Post a Comment