Method of C accessing access database through oledb
- 2021-07-03 00:47:51
- OfStack
This article illustrates how C # accesses access database through oledb. Share it for your reference. The specific analysis is as follows:
This demonstrates how to use the Microsoft Access database in C #. Includes how to create a dataset and add tables to the dataset from the database.
// OleDbSample.cs
// To generate this example from the command line, use the command:
// csc oledbsample.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Xml.Serialization;
public class MainClass
{
public static void Main ()
{
// Set the access connection and select the string.
// If you build this example from the command line,
// You must change the BugTypes.MDB Path of:
#if USINGPROJECTSYSTEM
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\BugTypes.MDB";
#else
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BugTypes.MDB";
#endif
string strAccessSelect = "SELECT * FROM Categories";
// Create a dataset and add to it Categories Table:
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch(Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet,"Categories");
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
// 1 A dataset can contain multiple tables, so you can put all these tables into
// 1 Array of:
DataTableCollection dta = myDataSet.Tables;
foreach (DataTable dt in dta)
{
Console.WriteLine("Found data table {0}", dt.TableName);
}
// The following two lines show two different methods that you can use to get a dataset
// Table count of:
Console.WriteLine("{0} tables in data set", myDataSet.Tables.Count);
Console.WriteLine("{0} tables in data set", dta.Count);
// The following lines show how to do this by name
// Getting information about a specific table from a dataset:
Console.WriteLine("{0} rows in Categories table", myDataSet.Tables["Categories"].Rows.Count);
// Column information is automatically extracted from the database, so
// You can see this information here:
Console.WriteLine("{0} columns in Categories table", myDataSet.Tables["Categories"].Columns.Count);
DataColumnCollection drc = myDataSet.Tables["Categories"].Columns;
int i = 0;
foreach (DataColumn dc in drc)
{
// Print the column subscript, and then print the name of the column and its
// Data type:
Console.WriteLine("Column name[{0}] is {1}, of type {2}",i++ , dc.ColumnName, dc.DataType);
}
DataRowCollection dra = myDataSet.Tables["Categories"].Rows;
foreach (DataRow dr in dra)
{
// Print CategoryID As a subscript, and then print CategoryName :
Console.WriteLine("CategoryName[{0}] is {1}", dr[0], dr[1]);
}
}
}
I hope this article is helpful to everyone's C # programming.