How to Use OLE With Visual C
The Microsoft OLE platform lets you connect to a database from your Visual C programs. The Microsoft Visual Studio software includes the necessary OLE libraries, so you just need to set up the project, create the reference to the libraries and query the database for the information. The process makes storing dynamic more efficient than older formats such as flat files.
Instructions
-
-
1
Open the Visual Studio software and the project you want to edit.
-
2
Add the OLE database libraries to the top of the file. You must include these libraries to connect using OLE. Copy and paste the following code to the top of the file:
using System.Data.OleDb;
-
-
3
Create the connection to the database. In this example, a connection to an Access database is created using the Access Jet OLE libraries. Add the following code to create the connection:
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.MDB";
OleDbConnection oleconn= null;
oleconn= new OleDbConnection(connect);Replace "db.mdb" with the name of your own Access database.
-
4
Query the database to retrieve the data using the OLE connection. The following code queries the database:
OleDbCommand cmd = new OleDbCommand("select * from customers",oleconn);
OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
oleconn.Open();
adapt.Fill(cmd);
-
1