How to Use an OleDBDataAdapter in C#
An OLE database adapter is a class used to connect to database software like Microsoft Access or Oracle. This class is used as an intermediate between the call to the database and the returned dataset. It standardizes the syntax, so calling any database for data sets does not require specific language coding. Instead, the programmer uses the intermediate adapter, which translates the different calls automatically.
Instructions
-
-
1
Create the connection string. The connection string is dependent on the type of database. In this example, the connection string is calling an Access database.
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myAccessDB.mdb;User Id=admin;Password=;"; -
2
Instantiate the OLE data adapter and connection classes. To use a class's methods and properties, it must be instantiated and assigned to a variable. The following code is an example of the syntax.
OleDbConnection myCon = new OleDbConnection(strConn);
OleDbDataAdapter myOleAdapter = new OleDbDataAdapter(); -
-
3
Send a command to the database. This is accomplished using the command class. The following code queries the database for all customers using the connection created in step two.
strQuery = "select * from customer";
myOleAdapter.SelectCommand = new OleDbCommand(strQuery, myCon); -
4
Create the dataset to hold the returned information. The dataset class is used to hold one or several records returned by the database.
DataSet myData = new DataSet(); -
5
Fill the dataset with the returned results from the query in step three. The OLE data adapter is used to fill the dataset with the records from the SQL query in step three. The following syntax shows how to fill the dataset.
myOleAdapter.Fill(myData);
-
1