This document describes two methods to connect to an Access database:CodeBoth methods and controls support encrypted databases. Both winform and ASP. NET are applicable.
Code Method, With the main source code: complete source code download
/// <Summary> /// Connect to the database /// </Summary> /// <Param name = "connstr"> connection string </param> Public Void Connaccess ( String Connstr ){ // Datadirectory project and directory. The generated directory is/debug/bin/. Obtain the path. Connstr =" Provider = Microsoft. Jet. oledb.4.0; Data Source = | datadirectory | \ dB \\ "; Connstr + =" Student1.mdb "; // Encrypt the database // Connstr + = "student2.mdb; Jet oledb: Database Password = ZZ "; Using (Oledbconnection conn = New Oledbconnection (connstr )){ Try {Oledbdataadapter da = New Oledbdataadapter (" Select * From t_user ", Conn); dataset DS = New Dataset (); da. Fill (DS ," T_user "); Maid [0]. defaultview ;} Catch (Exception ex) {MessageBox. Show (ex. Message );}}}
Control Mode, With the main source code: complete source code download
Procedure