// The following is my personal application configuration file App. config: overwrite all the content in App. config in the project.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConnectionStrings>
<Add name = "AccessDatabase"
ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = StoreMIS. mdb; Persist Security Info = false ;"
ProviderName = "System. Data. OleDb"/>
</ConnectionStrings>
</Configuration>
// Add the following two green namespaces for Reference
// Expand the ADO. NET project --> right-click the gray "Reference" folder --> Add reference -->. Net tab and pull down. Find System. Configuration and click "OK.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Configuration;
Using System. Data. OleDb;
// Write the code below
Namespace ADO. NET
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
ConnectionStringSettings connAccess; // used to map the connection string
// Access
Private void button#click (object sender, EventArgs e)
{
ListView1.Clear (); // when you click the button, clear the old data in listView1.
ConnAccess = ConfigurationManager. ConnectionStrings ["AccessDatabase"]; // The Name Of The connection string in the configuration file is enclosed in quotation marks.
If (connAccess! = Null)
{
String strCon = connAccess. ConnectionString;
This. textBox1.Text = strCon;
OleDbConnection conn = new OleDbConnection (strCon );
Try
{
Conn. Open ();
}
Catch (Exception ex)
{
This. textBox1.Text = ex. Message. ToString ();
}
If (conn. State = ConnectionState. Open)
{
// Retrieve data from the userinfo table in the StoreMIS. mdb Access database under the directory... \ bin \ Debug
ListView1.View = View. Details; // sets the display mode of the ListView View.
ListView1.Columns. Add ("name"). Width = 90; // Add a column name and specify the column Width
ListView1.Columns. Add ("password"). Width = 100;
ListView1.Columns. Add (""). Width = 150;
String SQL = "select userName, password, roleName from userinfo ";
OleDbCommand cmd = new OleDbCommand ("", conn );
Cmd. CommandText = SQL;
OleDbDataReader reader = cmd. ExecuteReader ();
While (reader. Read ())
{
String [] items = {reader [0]. ToString (), reader [1]. ToString (), reader [2]. ToString ()};
ListViewItem lvst = new ListViewItem (items );
Int num = listView1.Items. Count;
ListView1.Items. Insert (num, lvst );
}
Reader. Close ();
Conn. Close ();
Label1.ForeColor = Color. Red;
Label1.Text = "Access database connection successful! ";
}
Else
{
Label1.ForeColor = Color. Red;
Label1.Text = "An error occurred while connecting to the Access database! ";
}
}
}
}
}
// Window Interface Design
Is the result of clicking the button (it should be noted that the StoreMIS. mdb database under the output folder of the program must have the corresponding data)
// The application configuration file is used to connect to the database and read data from the database.
// Data of the userinfo table in StoreMIS. mdb
From floating clouds