1. Create an Access database table
1) Open Microsoft Access2010 in office, select the empty database to create the database Studentinfo.
2) Create a new table, select the data type in the first column of the table, and enter the column name.
3) Click Save, enter the database table name student, and complete the creation.
4) Copy the database you created to the debug file of your project to use.
2. Connect, open an Access database
1) Database Connection statement: public string ConnectionString = "Provider=microsoft.ace.oledb.12.0;data source= studentinfo.accdb";
2) Common query, execute SQL statement:
PublicDataSet Query (stringstrSQL) { using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) {DataSet DS=NewDataSet (); Try{Conn. Open (); OleDbDataAdapter command=NewOleDbDataAdapter (strSQL, conn); Command. Fill (DS,"DS"); } Catch(System.Data.SqlClient.SqlException ex) {Throw; } returnds; } } Public intExecuteSQL (stringstrSQL) { using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) { using(OleDbCommand cmd =NewOleDbCommand (strSQL, conn)) { Try{Conn. Open (); introws =cmd. ExecuteNonQuery (); returnrows; } Catch(System.Data.SqlClient.SqlException e) {conn. Close (); Throwe; } } } } Public ObjectGetsingle (stringstrSQL) { using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) { using(OleDbCommand cmd =NewOleDbCommand (strSQL, conn)) { Try{Conn. Open (); Objectobj =cmd. ExecuteScalar (); if(Object.Equals (obj,NULL)) ||(Object.Equals (obj, System.DBNull.Value))) { return NULL; } Else { returnobj; } } Catch(System.Data.SqlClient.SqlException e) {conn. Close (); Throwe; } } } }
View Code
3) Enter a common database SQL statement to complete the call.
Problems:
1) Add Accessdatabaseengine Data interface driver.
2) How to not recognize the. accdb format database, see:
Http://wenku.baidu.com/link?url= Qq3f2llz8gl9zkejhkae4l1bbeavcigo2wxer0mpvqxbpcqys5vgiczme2r1lpcsmd3beygnfi1lwrv4mlixhlaniwnzogstdkgtebnluum