Kirthika Murugavel Kirthika Murugavel - 8 months ago 57
C# Question

ASP.NET MVC without Entity Framework database connection

I have a project that uses ASP.NET MVC and follows three tier architecture. I want to add a class library that will have a base class which will contain the connection string and in the child class that connection string has to be called by pass the data source as string.

Base class:

public class DALConnection {
public SqlConnection con = new SqlConnection("Data Source=ABC;Initial Catalog=HotelMgmt;Integrated Security=True");
}


Child class:

public class DataAccessOperation: DALConnection {
// Some code
}


Instead of this I want to pass the connection string to the base class through child class as string.

Answer Source

Do you think you could use the constructors to do that? (as below)

Base class:

public class DALConnection {

     public SqlConnection con;

     public DALConnection(string connectionString)
     {
          con = new SqlConnection(connectionString);
     }

}

Child class:

public class DataAccessOperation: DALConnection {

    //Some code
    public DataAccessOperation() : base("Data Source=RAV-DSK-581;Initial Catalog=HotelMgmt;Integrated Security=True")
    {

    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download