C # SQLite database operations

Source: Internet
Author: User
Tags sqlite sqlite database

WinCE Project Development VS2008

Write your own SQLite database management class code is as follows:

SQLiteManager.cs

usingSystem.Data;usingSystem.Data.SQLite;usingSystem.IO;usingSystem.Reflection;namespaceteachhmi{ Public classSqlitemanager { PublicSqlitemanager (stringDataBaseName,stringdatatablename) {             This. DbName =DataBaseName;  This. Dtname =dataTableName; }        //Database file name        stringDbName; //data table name        stringDtname; stringAppPath =Path.getdirectoryname (assembly.getexecutingassembly (). GetName ().        CodeBase); //Database Connection Statements        stringconnstr; //Database Connection         PublicSqliteconnection Conn; //Sqlitecommand instance, used to issue instructions to SQLitesqlitecommand command; //Sqlitedataadapter instance for populating database data into a datasetSqlitedataadapter Adapter; //Sqlitedatareader instances for reading data from a data tableSqlitedatareader Reader; /// <summary>        ///Database Connection/// </summary>         Public voidInitializesqlite () { This. ConnStr ="Data source="+ AppPath +"\\"+ DbName +"; Pooling=true; Failifmissing=false"; Conn=Newsqliteconnection (CONNSTR); //Open ConnectionConn.        Open (); }        /// <summary>        ///Create a data table/// </summary>        /// <param name= "CreateCommand" ></param>         Public voidCreateTable (stringCreateCommand) {Command=NewSqlitecommand (conn); Command.commandtext="CREATE TABLE IF not EXISTS"+ Dtname +CreateCommand; Command.        ExecuteNonQuery (); }        /// <summary>        ///executes the SQL command, returning the number of rows affected/// </summary>        /// <param name= "Command" >Command Statements</param>         Public voidExenonquerycmd (stringCommand) {Command=NewSqlitecommand (conn); Command.commandtext=Command; Command.        ExecuteNonQuery (); }        /// <summary>        ///Execute SQL command, return sqlitedatareader/// </summary>        /// <param name= "Command" ></param>        /// <returns></returns>         PublicSqlitedatareader Exereadercmd (stringCommand) {Command=NewSqlitecommand (conn); Command.commandtext=Command; Reader=command.  ExecuteReader (); return type is Sqlitedatareaderreturnreader; }        /// <summary>        ///Populating a DataSet/// </summary>        /// <param name= "ds" ></param>         Public voidDatafill (DataSet ds) {Adapter=NewSqlitedataadapter ("SELECT * from"+Dtname, conn); Adapter.        Fill (ds, Dtname); }    }}

To read the data in a cell in a data table:

 Public StaticSqlitemanager Sqllog; Public StaticSqlitedatareader Reader;intNum =int. Parse (Datagrid1[datagrid1.currentrowindex,0]. ToString ());stringSearchcmd ="SELECT * from InfoLog WHERE ordinal ="+ Num +""; Reader=Sqllog.exereadercmd (Searchcmd); Reader.read (); listview1.items[0]. subitems[1]. Text = reader.getstring (7); Get the data in the column with index 7 in this record with the ordinal number Num in the datasheet listview1.items[1]. subitems[1]. Text = reader.getstring (8); Reader.close ();
Insert directive: INSERT INTO UserInfo VALUES 1, Xiao Li
Update directive: Updated UserInfo SET user id=1
Delete directive: Delete from UserInfo WHERE user id=2, name = ' Xiao Li '

C # SQLite database operations

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.