SqlLiteデータベースヘルプクラスと基本DEMO
23456 ワード
title:SqlLiteデータベースヘルプ類と基本DEMO categories:Codeing date:2019-10-15:05:13 tags:[C菵、プログラミング開発、実用教程、DEMO]thumbnail:http://hemrj.cn/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_2010152920.jpg---
SqlLiteデータベースヘルプクラスと基本DEMO
SQLliteのメリット
NuGet管理プログラム-->ブラウズ-->System.Data.SQLiteを検索して、参照をインストールします。
ここで直接ダウンロードして、System.Data.SQLite.dllを参照して、SQLite.Interop.dll.zipをdebugディレクトリの下に置いてください。
1.http://hemrj.cn/System.Data.SQLite.dll 2.http://hemrj.cn/SQLite.Interop.dll.zip
Sql Liteヘルプクラス
SqlLiteデータベースヘルプクラスと基本DEMO
SQLliteのメリット
◇
SQLite C/S , , 。 SQLite , 。 , 3.6.11 ,Windows 487KB、Linux 347KB。
◇
SQLite : , “ ”。 。
◇
“ ”, ( 、 、 、 ) 。 copy , 。
◇ /
, 。 ,SQLite 。 ( Android、WindowsMobile、Symbin、Palm、VxWorks ) 。
◇ (in-memory database)
, , PC GB ( )。 ,SQLite 。
SQLite API ( )。 I/O , 。 , SQLite , Load , Dump OK 。 , “onlinebackup API” , backup ?
参照を追加NuGet管理プログラム-->ブラウズ-->System.Data.SQLiteを検索して、参照をインストールします。
ここで直接ダウンロードして、System.Data.SQLite.dllを参照して、SQLite.Interop.dll.zipをdebugディレクトリの下に置いてください。
1.http://hemrj.cn/System.Data.SQLite.dll 2.http://hemrj.cn/SQLite.Interop.dll.zip
Sql Liteヘルプクラス
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Nine.UnitlLibrary
{
///
/// SQLite
/// :
/// Execute,Save,Update,Delete...
/// @author Nine
///
public class SqlLiteHepler
{
private bool _showSql = true;
///
/// SQL
///
public bool ShowSql
{
get
{
return this._showSql;
}
set
{
this._showSql = value;
}
}
private readonly string _dataFile;
private SQLiteConnection _conn;
public SqlLiteHepler(string dataFile)
{
if (dataFile == null)
throw new ArgumentNullException("dataFile=null");
this._dataFile = dataFile;
}
///
/// SQLiteManager
///
public void Open()
{
this._conn = OpenConnection(this._dataFile);
}
public void Close()
{
if (this._conn != null)
{
this._conn.Close();
}
}
///
/// ,
///
public void CloseQuietly()
{
if (this._conn != null)
{
try
{
this._conn.Close();
}
catch { }
}
}
///
/// SQLiteConnection, Open
/// ,
///
///
///
public static SQLiteConnection OpenConnection(string dataFile)
{
if (dataFile == null)
throw new ArgumentNullException("dataFile=null");
if (!File.Exists(dataFile))
{
SQLiteConnection.CreateFile(dataFile);
}
SQLiteConnection conn = new SQLiteConnection();
SQLiteConnectionStringBuilder conStr = new SQLiteConnectionStringBuilder
{
DataSource = dataFile
};
conn.ConnectionString = conStr.ToString();
conn.Open();
return conn;
}
///
/// SQLiteManager
///
public SQLiteConnection Connection
{
get
{
return this._conn;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
this._conn = value;
}
}
protected void EnsureConnection()
{
if (this._conn == null)
{
throw new Exception("SQLiteManager.Connection=null");
}
}
public string GetDataFile()
{
return this._dataFile;
}
///
/// table
///
///
///
public bool TableExists(string table)
{
if (table == null)
throw new ArgumentNullException("table=null");
this.EnsureConnection();
// SELECT count(*) FROM sqlite_master WHERE type='table' AND name='test';
SQLiteCommand cmd = new SQLiteCommand("SELECT count(*) as c FROM sqlite_master WHERE type='table' AND name=@tableName ");
cmd.Connection = this.Connection;
cmd.Parameters.Add(new SQLiteParameter("tableName", table));
SQLiteDataReader reader = cmd.ExecuteReader();
reader.Read();
int c = reader.GetInt32(0);
reader.Close();
reader.Dispose();
cmd.Dispose();
//return false;
return c == 1;
}
///
/// SQL,
///
/// paramArr == null
///
///
///
public int ExecuteNonQuery(string sql, SQLiteParameter[] paramArr)
{
if (sql == null)
{
throw new ArgumentNullException("sql=null");
}
this.EnsureConnection();
if (this.ShowSql)
{
Console.WriteLine("SQL: " + sql);
}
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = sql;
if (paramArr != null)
{
foreach (SQLiteParameter p in paramArr)
{
cmd.Parameters.Add(p);
}
}
cmd.Connection = this.Connection;
int c = cmd.ExecuteNonQuery();
cmd.Dispose();
return c;
}
///
/// SQL, SQLiteDataReader
/// Reader , Read()
/// paramArr=null,
///
///
///
///
public SQLiteDataReader ExecuteReader(string sql, SQLiteParameter[] paramArr)
{
return (SQLiteDataReader)ExecuteReader(sql, paramArr, (ReaderWrapper)null);
}
///
/// SQL, readerWrapper!=null, readerWrapper SQLiteDataReader ,
///
///
/// null
/// null SQLiteDataReader
///
public object ExecuteReader(string sql, SQLiteParameter[] paramArr, ReaderWrapper readerWrapper)
{
if (sql == null)
{
throw new ArgumentNullException("sql=null");
}
this.EnsureConnection();
SQLiteCommand cmd = new SQLiteCommand(sql, this.Connection);
if (paramArr != null)
{
foreach (SQLiteParameter p in paramArr)
{
cmd.Parameters.Add(p);
}
}
SQLiteDataReader reader = cmd.ExecuteReader();
object result = null;
if (readerWrapper != null)
{
result = readerWrapper(reader);
}
else
{
result = reader;
}
reader.Close();
reader.Dispose();
cmd.Dispose();
return result;
}
///
/// SQL, , RowWrapper
/// , List (List.Count=0)
/// rowWrapper = null , WrapRowToDictionary
///
///
///
///
///
public List
Sql Lite DEMOusing System;
using System.Data.SQLite;
using System.Windows;
namespace SqlLiteHelper
{
///
/// MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlLiteHepler sqlLiteHepler = new SqlLiteHepler(@"D:\NineBackstageToolsDB.db");
sqlLiteHepler.Open();
//insert
string insertSql = "INSERT INTO CountUserData(JsonData) values (@JsonData)";
SQLiteParameter[] insertParameters = new SQLiteParameter[]{
new SQLiteParameter("@JsonData","test") };
int insertRowCount = sqlLiteHepler.ExecuteNonQuerySql(insertSql, insertParameters);
//update
string updateSql = "Update CountUserData set JsonData= @JsonData where id = @ID";
SQLiteParameter[] updateParameters = new SQLiteParameter[]{
new SQLiteParameter("@JsonData","update"),
new SQLiteParameter("@ID","3")
};
int updateRowCount = sqlLiteHepler.ExecuteNonQuerySql(updateSql, updateParameters);
//delete
string deleteSql = "delete from CountUserData where id = @ID";
SQLiteParameter[] deleteParameters = new SQLiteParameter[]{
new SQLiteParameter("@ID","4")
};
int deleteRowCount = sqlLiteHepler.ExecuteNonQuerySql(deleteSql, deleteParameters);
//select
string sqlText = "select * from XyhisLog where (logtime) > (@logtime)";
SQLiteParameter[] selectParameters = new SQLiteParameter[]{
new SQLiteParameter("@logtime",DateTime.Now.Date.ToString().Replace("/","-")) };
var ss = sqlLiteHepler.ExecuteDataTable(sqlText, selectParameters);
}
}
}
Sql LiteHelper Demoソースのダウンロードhttp://hemrj.cn/SqlLiteHelper.zip