none
Class question w SQL RRS feed

  • Question

  • I am just trying to figure out how to use classes with SQL. I have a Table called mileagelog where users can log miles they ride on bikes.  It consists of a number of columns:

    ride id INT

    ridedate Datetime

    distance float

    riderid int

    I have setup a C# class called stats

    public class stats
    {
        private int _rideid;
        public int rideid {get; set;}
        private int _riderid;
        public int riderid { get;  set;}
        private DateTime _ridedate;
        public DateTime ridedate { get;  set;}
        private float _distancemiles;
        public float distancemiles{ get; set;}
    }

    There is a stored procedure that deletes  a ride

    DELETE mileagelog where rideid = @rideid

    To do the delete in the past the method was public and I just passed the ride id into it.

    public void DeleteRide(int rideid)
        {
    
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "umDeleteRide";
            cmd.Parameters.Add("@rideid", SqlDbType.Int).Value = rideid;
            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                string err = ex.ToString();
            }
            finally
            {
                conn.Close();
            }
        }

    How to I   call that method when I pass in a public rideid  And the method is private?

     


    • Edited by Dave Parker Friday, May 2, 2014 9:21 PM
    • Moved by Bob Beauchemin Saturday, May 3, 2014 5:13 PM Moving to the forum for SqlClient for best response
    Friday, May 2, 2014 9:20 PM

Answers

  • In the best practices, you must make the properties in the persisted class has the name like the table name, and make the business rules methods in the separate class has the name like the name of persisted class + (BLL) suffixe (its an example) :

    Table = Customer.

    Persisted Class = Customer (properties).

    Business Class = CustomerBLL (add, update, delete, get, getList...).

    So to call one method like (delete) from the web page or Window form in the same project, you must use (Protected). And if you want to call this method from another project you must use (Public).

    Friday, May 2, 2014 11:28 PM

All replies

  • Are you trying to use the class from the client or server side? I.e. do you mean to call DeleteRide as a SQLCLR stored procedure (if so, the method must be public).

    If this is .NET client-side code, the best forum is MSDN ADO.NET Data Providers forum (http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetdataproviders); if that's the case, let me know and I'll move it over there.

    Cheers, Bob

    Friday, May 2, 2014 11:13 PM
  • In the best practices, you must make the properties in the persisted class has the name like the table name, and make the business rules methods in the separate class has the name like the name of persisted class + (BLL) suffixe (its an example) :

    Table = Customer.

    Persisted Class = Customer (properties).

    Business Class = CustomerBLL (add, update, delete, get, getList...).

    So to call one method like (delete) from the web page or Window form in the same project, you must use (Protected). And if you want to call this method from another project you must use (Public).

    Friday, May 2, 2014 11:28 PM