Help On Object Oriented Programming

  • Thread starter Thread starter Naren
  • Start date Start date
N

Naren

Hi,
i am developing one application,that is completely
object oriented application.the main purpose of this
applicaiton to completely maintain the database
operations for the project.
i created one class for the database connection(sql
server database).i want to use that database connection
thorough out all classes in the application,how can i
assign the database connection to the alll the classes.
any one can help me with one example.
the example like this

public class database
{
connection.open()
}



public class employee
{
public void insertnewemployee()
{
//here how can i get that database connection
}
}

Thanks in advance.
Regards
Naren.
 
Hi Naren

Maybe try this:

Database class:
----------------

public class database
{
public SqlConnection myConn;
public database()
{
myConn = new SqlConnection(..);
myConn.Open();
}
}

Employee Class
----------------

public class Employee
{
public SqlConnection myEmpCon;
public Employee()
{
database myDB = new database();
myEmpCon = myDB.myConn;
}

public void InsertNewEmployee()
{
//Use myEmpCon here.
}
}

Hope this helps

Cobus Lombard
Application Developer
Mint Net
South Africa
 
Hi Cobus,
Thanks for u r mail.i tested that application.butin employee class, i
am not getting the connection object.in this i wrote the code.

in database class
public class database
{
public SqlConnection con
database.username="ass"
databse.password="asdas"
database.server="asdas"
database.catalog="cad"
database.connect()
{
con = new SqlConnection("user id=;password=;...")
con.open()
}
}

in Employee class
public class Employee
{
public SqlConnection empcon
public Employee()
{
database d = new database()
//but i am not getting the connection here...the variable is undeclared.
empcon = d.con;
}
}

after completing this,i used this code in form.
by using this way i implemented.

in user interface

Dim emp As Employee
Dim db As databse
db = New database()
db.Connect("sdf", "s", "d", "d")
emp = New Employee
//but i am not getting the connection status here

how can i get the connection status.
Thanks in advance.

Thanks
Naren
 
Why not create a data layer that can be accessed throughout your app! I
have created a generic database classes for MSSQL, MySQL which my DALC
access to comunicate with the datasource. In the DALC (data access layer
components) I create a base class that reads in the connection string from a
config file and just have all your DALC inherit from the base class in which
you can create your connection.

Good Luck
 
Back
Top