SQLite is a good choice for Windows Mobile databases. SQLite is: SQLite, and SQLite's ado.net provider is: system. data. SQLite. Do not forget to copy SQLite when publishing a program. interop.065.dll, system. data. SQLite. DLL files to the installation directory.
This article is not original, but refers to access SQLite (a solution to replace SQL CE) under WM written by egmkang, and makes some modifications according to my programming needs and habits, errors are inevitable. The following is a sqlitehelper encapsulation class:
Using system. data; using system. data. SQLite; using system. io; namespace myhelper. dataaccess {public class sqlitehelper {Private Static string Password = "***"; // change *** to Private Static string dbfilepath = path. getdirectoryname (system. reflection. assembly. getexecutingassembly (). getname (). codebase) + "\\***. DB "; // modify *** to Private Static string connectstring = string. format ("Data Source = \ "{0} \" ", dbfilepath, password); Private Static sqliteconnection myconnect = new sqliteconnection (connectstring ); /** // <summary> // obtain the current SQLite connection // </Summary> // <returns> current SQLite connection </returns> Public static sqliteconnection getconnection () {return myconnect;}/** // <summary> // execute the SQL statement, returns the number of affected rows /// </Summary> /// <Param name = "commandstring"> SQL statement </param> /// <Param name = "Parameters"> SQL language Sentence parameter </param> /// <returns> affected rows </returns> Public static int executenonquery (string commandstring, Params sqliteparameter [] parameters) {int result = 0; using (sqlitecommand command = new sqlitecommand () {preparecommand (command, null, commandstring, parameters); Result = command. executenonquery (); command. parameters. clear ();} return result;}/** // <summary> // execute the SQL statement with the transaction and return the number of affected rows. // </sum Mary> /// <Param name = "transaction"> SQL transaction </param> /// <Param name = "commandstring"> SQL statement </param> // <param name = "Parameters"> SQL statement parameters </param> // <returns> affected rows </returns> Public static int executenonquery (sqlitetransaction transaction, string commandstring, Params sqliteparameter [] parameters) {int result = 0; using (sqlitecommand command = new sqlitecommand () {preparecommand (command, transacti On, commandstring, parameters); Result = command. executenonquery (); command. parameters. clear ();} return result;}/** // <summary> // execute the query and return the value of the first column of the first row of the result set, ignore all other rows and columns /// </Summary> /// <Param name = "commandstring"> SQL statement </param> /// <Param name = "Parameters "> SQL statement parameters </param> // <returns> values in the first column of the First row </returns> Public static object executescalar (string commandstring, params sqliteparameter [] parameters) {Object result; using (sqlitecommand command = new sqlitecommand () {preparecommand (command, null, commandstring, parameters); Result = command. executescalar ();} return result;}/** // <summary> // execute the SQL statement, returned result set datareader /// </Summary> /// <Param name = "commandstring"> SQL statement </param> /// <Param name = "Parameters"> SQL statement parameters </param> /// <returns> datareader of the result set </returns> Public static sqlitedatare Ader executereader (string commandstring, Params sqliteparameter [] parameters) {sqlitecommand command = new sqlitecommand (); try {preparecommand (command, null, commandstring, parameters); sqlitedatareader reader = command. executereader (commandbehavior. closeconnection); command. parameters. clear (); Return reader;} catch {Throw;}/** // <summary> // preprocessing command object, database link, transaction, initialization of objects and parameters to be executed /// </Summary> /// <Param name = "command"> command object </param> /// <Param name = "transaction"> transaction object </param> /// <Param name = "commandstring"> SQL statement </param> // <Param name = "Parameters"> SQL statement parameter </param> Private Static void preparecommand (sqlitecommand command, sqlitetransaction transaction, string commandstring, Params sqliteparameter [] parameters) {If (myconnect. state! = Connectionstate. Open) myconnect. open (); command. Connection = myconnect; command. commandtext = commandstring; If (transaction! = NULL) command. Transaction = Transaction; If (parameters! = NULL & parameters. length> 0) {command. Parameters. addrange (parameters );}}}}
To improve efficiency, I changed the database connection to a persistent connection. In this way, only the first connection will be time-consuming, and subsequent operations will be faster. If there is any error in this article, please refer to the original article egmkang.