--Before using, be careful to change "configuration platform" of "Configuration Manager" in "Build" to X86
Private voidButton1_Click (Objectsender, EventArgs e)//Enquiry{OleDbConnection conn=NewOleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\score.mdb; Jet oledb:database password=");//Connecting the Access2003 database stringOleDbCommand cmd =Conn. CreateCommand (); Conn. Open (); Cmd.commandtext="Select * FROM score"; OleDbDataReader Dr=cmd. ExecuteReader (); DataTable DT=NewDataTable ();//Create a temporary table to store the data if(Dr. HasRows) {//Adding Columns for(inti =0; I < Dr. FieldCount; i++) {dt. Columns.Add (Dr. GetName (i));//Dr. GetName (i) Get column names} dt. Rows.clear (); //Adding Rows while(Dr. Read ())//While the loop is the data of each row, as long as there is data in the database, it will be read one line at a time, each read a row to execute while{DataRow row=dt. NewRow (); for(inti =0; I < Dr. FieldCount; i++)//for Loop loops is the data that determines the column after a row{Row[i]= Dr[i];//i is the index//MessageBox.Show (Dr[i]. ToString ());} dt. Rows.Add (row); }} conn. Close (); Datagridview1.datasource= DT;//Data Source}
、、、、、 below is how to create a table in an Access database
Connect to an Access database query statement