Page 96 - DCAP312_WEB_TECHNOLOGIES_II
P. 96
Web Technologies-II
Notes this.textboxLastname.DataBindings .Add( “Text”, datc.dSet.Tables[
“PersonTable”], “LastName”);
this.textboxTitle.DataBindings.Add( “Text”, datc.dSet.Tables[ “PersonTable”],
“Title”);
this.textboxCity.DataBindings.Add( “Text”, datc.dSet.Tables[ “PersonTable”],
“City”);
this.textboxCountry.DataBindings.Add(“Text”,datc.dSet.
Tables[“PersonTable”], “Country”);
}
The term Object Database first introduced in 1985.
5.2 Database Connection
Connecting to an Access database requires a different connection object and connection string.
Access uses something called OLEDB, so you need to create a database object of this type. Double
click your form. Outside of the form load event, type the following:
System.Data.OleDb.OleDbConnection con;
Inside of the form load event, type this:
con = new System.Data.OleDb.OleDbConnection();
This sets up a new connection object called con. It is an OLEDB connection object. The connection
string can be found using the same technique as for SQL Server Express, just outlined. Once the
string is pasted over, it will look like this:
con.ConnectionString = “PROVIDER=Microsoft.Jet.OLEDB.4.0;
Data Source=C:/Workers.mdb”;
(Notice that you can use a single forward slash in a file name, but not a single backslash.)
The Provider you use for Access databases is called Microsoft Jet. We are using version 4.0 in
the code above. Again, we need to tell C# where the database is. This is done with Data Source.
The source above is pointing to a database called Workers.mdb that is on the C drive.
The connection to the database is done with the Open method of your connection object:
con.Open();
Close the connection in a similar way:
con.Close();
You can also issue a Dispose command at the end, if you want. This will do the “tidying up”
for you:
con.Dispose();
Add a few message boxes and your coding window should look like this:
System.Data.Oledb.OleDbConnection con;
Private void Form1_load(object sender, EventArgs e)
{
Con=new System.Data.OleDb.OleDbConnection();
Con.ConnectionString= “PROVIDER=Microsoft.Jet.OLEDB.4.0;
90 LOVELY PROFESSIONAL UNIVERSITY