Detailed explanation of connecting ACCESS database instance by C programming
- 2021-08-31 08:55:51
- OfStack
This paper describes the method of connecting ACCESS database by C # programming. Share it for your reference, as follows:
1. Create an FORM form, add a button control and add an DATAGRIDVIEW control.
2. Double-click FORM to add a namespace
using System.Data.OleDb;
Double-click the button to enter the button code and write the following code
OleDbConnection strConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + " Employee information .mdb" + ";Persist Security Info=False");
// Establish a database engine connection, paying attention to the data table (suffix is .db ) should be placed in DEBUG Under file
OleDbDataAdapter myda = new OleDbDataAdapter("select * from Employees ,strConnection);
// Create an adapter through the SQL Statement to search the database
DataSet myds = new DataSet();
// Create a dataset
myda.Fill(myds, " Employees ");
// Use FILL To populate the data set with the data table that the adapter has connected MYDS This table
dataGridView1.DataSource = myds.Tables[" Contact person ID"];
// Display tables with display controls
3. After pressing F5 to run, click BUTTON button, and the table in the database under the corresponding SQL statement will be displayed.
The following uses Command and reader objects to output data under the console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
OleDbConnection mycon =null;
OleDbDataReader myReader=null;
try
{
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db4.mdb;";
mycon = new OleDbConnection(strcon);
mycon.Open();
string sql = "select * from Employees ";
OleDbCommand mycom = new OleDbCommand(sql, mycon);
myReader = mycom.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader.GetString(0)+" "+myReader.GetDouble(1)+" "+myReader.GetString(2)+" "+myReader.GetString(3)+" "+myReader.GetString(4));
}
}
finally
{
myReader.Close();
mycon.Close();
}
}
}
}
I hope this article is helpful to everyone's C # programming.