Recently WPF do clients need to store data offline, consider using Sqlite embedded database in the project, the Internet to find a lot of information, and finally sorted out the help of a public class.
Sqlite is a very small database, basically have most of the features of relational database operations, Sql syntax similar. Here is my sort of help class code:
1. Get S QLiteConnection object passed to the address database.
/// <summary>
/// 获得连接对象
/// </summary>
/// <returns>SQLiteConnection</returns>
public static SQLiteConnection GetSQLiteConnection()
{
//Sqlite数据库地址
string str = AppDomain.CurrentDomain.BaseDirectory;
var con = new SQLiteConnection("Data Source=" + str + "DataBass\\InfoServiceDbB.db");
return con;
}
2. Prepare operational command parameters, structure SQLiteComma Nd objects:
/// <summary> /// 准备操作命令参数 /// </summary> /// <param name="cmd">SQLiteCommand</param> /// <param name="conn">SQLiteConnection</param> /// <param name="cmdText">Sql命令文本</param> /// <param name="data">参数数组</param> private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, Dictionary<String, String> data) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = cmdText; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 30; if (data!=null&&data.Count >= 1) { foreach (KeyValuePair<String, String> val in data) { cmd.Parameters.AddWithValue(val.Key, val.Value); } } }
3. query, returns a DataSet
/// <Summary> /// query, returns the DataSet /// </ Summary> /// <param name = "cmdText The"> the Sql command text </ param> /// <param name = "Data"> Parameter array </ param> /// <Returns> the DataSet </ Returns> public static the DataSet the ExecuteDataset ( String cmdText The, the Dictionary < String , String > Data) { var DS = new new the DataSet (); using (SQLiteConnection connection = GetSQLiteConnection()) { var command = new SQLiteCommand(); PrepareCommand(command, connection, cmdText, data); var da = new SQLiteDataAdapter(command); da.Fill(ds); } return ds; }
4. query returns a DataTable
/// <Summary> /// query, returns the DataTable /// </ Summary> /// <param name = "cmdText The"> the Sql command text </ param> /// <param name = "Data"> Parameter array </ param> /// <Returns> the DataTable </ Returns> public static the DataTable ExecuteDataTable ( String cmdText The, the Dictionary < String , String > Data) { var dt = new new the DataTable (); using (SQLiteConnection connection = GetSQLiteConnection()) { var command = new SQLiteCommand(); PrepareCommand(command, connection, cmdText, data); SQLiteDataReader reader = command.ExecuteReader(); dt.Load(reader); } return dt; }
5. Return the data row DataRow
/// <summary> /// 返回一行数据 /// </summary> /// <param name="cmdText">Sql命令文本</param> /// <param name="data">参数数组</param> /// <returns>DataRow</returns> public static DataRow ExecuteDataRow(string cmdText, Dictionary<string, string> data) { DataSet ds = ExecuteDataset(cmdText, data); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) return ds.Tables[0].Rows[0]; return null; }
6. perform database operations
/// <Summary> /// perform database operations /// </ Summary> /// <param name = "cmdText The"> the Sql command text </ param> /// <param name = "Data"> Incoming parameters </ param> /// <returns> returns the number of rows affected </ returns> public static int the ExecuteNonQuery ( String cmdText the, the Dictionary < String , String > Data) { the using (SQLiteConnection Connection = GetSQLiteConnection ()) { var = the Command new new SQLiteCommand(); PrepareCommand(command, connection, cmdText, data); return command.ExecuteNonQuery(); } }
7. Return the SqlDataReader object
/// <summary> /// 返回SqlDataReader对象 /// </summary> /// <param name="cmdText">Sql命令文本</param> /// <param name="data">传入的参数</param> /// <returns>SQLiteDataReader</returns> public static SQLiteDataReader ExecuteReader(string cmdText, Dictionary<string, string> data) { var command = new SQLiteCommand(); SQLiteConnection connection = GetSQLiteConnection(); try { PrepareCommand(command, connection, cmdText, data); SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); return reader; } catch { connection.Close(); command.Dispose(); throw; } }
8. Return the result set first row and first column, ignoring other columns or rows
/// <Summary> /// Returns result set first row and first column, ignoring other columns or rows /// </ Summary> /// <param name = "cmdText The"> the Sql command text </ param> /// <param name = "Data"> parameters passed </ param> /// <Returns> Object </ Returns> public static Object the ExecuteScalar ( String cmdText the, the Dictionary < String , String > Data) { the using (SQLiteConnection = Connection GetSQLiteConnection ()) { var cmd = new new SQLiteCommand(); PrepareCommand(cmd, connection, cmdText, data); return cmd.ExecuteScalar(); } }
9. paging query
/// <Summary> /// paging query /// </ Summary> /// <param name = "recordCount"> Total Records </ param> /// <param name = "the pageIndex"> page traction < / param> /// <param name = "pageSize"> page size </ param> /// <param name = "cmdText The"> Sql command text </ param> /// <param name = "countText"> query total number of recording text Sql </ param> /// <param name = "Data"> command parameters </ param> /// <Returns> the DataSet </ Returns> public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, Dictionary<string, string> data) { if (recordCount < 0) recordCount = int.Parse(ExecuteScalar(countText, data).ToString()); var ds = new DataSet(); using (SQLiteConnection connection = GetSQLiteConnection()) { var command = new SQLiteCommand(); PrepareCommand(command, connection, cmdText, data); var da = new SQLiteDataAdapter(command); da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result"); } return ds; }
10. reorganize the database
When you delete data from a SQLite database, unused disk space will be added to the "free list" in an internal.
The next time you insert data, this space can be reused. Disk space is not lost, but it will not be returned to the operating system.
If you delete a lot of data, and want to reduce the space occupied by the database files, perform the VACUUM command. VACUUM will reorganize the database from scratch,
you can agree on a time interval to perform the operation once to reorganize the database in your program, save space
public void ResetDataBass() { using (SQLiteConnection conn = GetSQLiteConnection()) { var cmd = new SQLiteCommand(); if (conn.State != ConnectionState.Open) conn.Open(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = "vacuum"; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 30; cmd.ExecuteNonQuery(); } }
Reproduced in: https: //www.cnblogs.com/OnlyVersion/p/3746086.html