ADO. NET database programming reads data using application configuration files

Source: Internet
Author: User
Tags mdb database


// 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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.