SQLite access encapsulation class)

Source: Internet
Author: User

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.

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.