Friday 19 April 2013

DA

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AppDBCon"].ToString());
       
        public int Save(SampleBO objSave)
        {
            SqlCommand objCmd = new SqlCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "sp_Save";
            try
            {
                objCmd.Parameters.AddWithValue("P_Id",objSave.ID == 0? (object)DBNull.Value:objSave.ID);
                objCmd.Parameters.AddWithValue("P_Name", objSave.Name);
                objCmd.Parameters.AddWithValue("P_Address", objSave.Address);
                objCmd.Parameters.AddWithValue("P_Phone", objSave.Phone);
                objCmd.Parameters.AddWithValue("P_Remarks", objSave.Remarks);
                SqlParameter outParms = new SqlParameter("@P_RetVal",SqlDbType.Int);
                outParms.Direction = ParameterDirection.Output;
                objCmd.Parameters.Add(outParms);
                objCmd.Connection = con;
                con.Open();
                objCmd.ExecuteNonQuery();
                con.Close();
                return Convert.ToInt32(outParms.Value);
            }
            catch(Exception ex)
            {
                return -1;
            }
            
        }

        public DataSet GetDetails()
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_GetDetails";
            try
            {
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                adp.Fill(ds);
            }
            catch(Exception ex)
            {
                
            }
            return ds;
        }

        public int Delete(int id)
        {
            SqlCommand objCmd = new SqlCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "spDelete";
            try
            {
                objCmd.Parameters.AddWithValue("P_Id", id == 0 ? (object)DBNull.Value : id);
                SqlParameter outParms = new SqlParameter("@P_RetVal", SqlDbType.Int);
                outParms.Direction = ParameterDirection.Output;
                objCmd.Parameters.Add(outParms);
                objCmd.Connection = con;
                con.Open();
                objCmd.ExecuteNonQuery();
                con.Close();
                return Convert.ToInt32(outParms.Value);
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

No comments:

Post a Comment