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