C# SQlite操作方法小結
本文實例分析了C# SQlite操作方法。分享給大家供大家參考,具體如下:
最近項目需求用C#保存一些數(shù)據(jù),如此先總結一下。需要下載Sqlite 庫 SourceForge 鏈接網(wǎng)址http://sourceforge.net/projects/sqlite-dotnet2/或到官方網(wǎng)http://www.sqlite.org/download.html下載都可以,下載之后安裝。在C#項目中添加引用 引入安裝目錄bin中的System.Data.SQLite.dll。添加命名空間using System.Data.SQLite;便可以在你的項目中
對擴平臺的微型數(shù)據(jù)庫SQlite 進行使用了,
主要注意一點是:
數(shù)據(jù)庫若未創(chuàng)建則使用:
SQLiteConnection.CreateFile(databaseName);
數(shù)據(jù)庫已經(jīng)創(chuàng)建,并要進行訪問:
下面是項目中封裝的操作數(shù)據(jù)庫代碼,使用時可稍微修改便可在項目中使用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
namespace Toolbar
{
public class CSPDatabase
{
protected string m_dbName;
protected string m_tablename;
protected string m_password;
public CSPDatabase(string dbName)
{
m_dbName = dbName;
m_tablename = "MhtInfo";
m_password = "";
}
//Create DataBase
public virtual void Init() { }
public virtual void CreateDataBase() { }
public virtual void OpenDataBase() { }
public virtual void SetPassWord(string password) { }
//Connect DataBase
public virtual void ConnectDataBase() { }
//Create Table
public virtual void CreateTable(string tableName) { }
//Insert Data
public virtual void Insert(string mhtlocation) { }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.Windows.Forms;
namespace Toolbar
{
class SqliteDatabase : CSPDatabase
{
private SQLiteConnection m_conn= null;
private SQLiteCommand m_cmd=null;
public SqliteDatabase(string dbName):base(dbName)
{
}
public override void Init()
{
if(m_conn == null)
m_conn = new SQLiteConnection();
m_cmd = new SQLiteCommand();
m_cmd.Connection = m_conn;
}
public override void CreateDataBase()
{
//Create Database
try
{
SQLiteConnection.CreateFile(m_dbName);
Init();
ConnectDataBase();
}
catch (System.Exception e)
{
MessageBox.Show("Create DataBase Failed!");
}
}
public override void OpenDataBase()
{
m_conn = new SQLiteConnection("Data Source="+m_dbName+";Version=3;New=False;Compress=True;");
Init();
ConnectDataBase();
}
public override void SetPassWord(string password)
{
m_password = password;
}
public override void ConnectDataBase()
{
//Connect to DataBase
try
{
SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
connstr.DataSource = m_dbName;
if(m_password != "")
connstr.Password = m_password;
m_conn.ConnectionString = connstr.ToString();
}
catch (System.Exception e)
{
MessageBox.Show("Fail to Connect to the database");
}
}
//Create Table
public override void CreateTable(string tableName)
{
try
{
m_tablename = tableName;
m_conn.Open();
string sql = "CREATE TABLE " + tableName + "(mhtlocation varchar(20))";
m_cmd.CommandText = sql;
m_cmd.ExecuteNonQuery();
m_conn.Close();
}
catch (System.Exception e)
{
MessageBox.Show("Create Table Failed!");
}
}
public override void Insert(string mhtlocation)
{
try
{
//Insert Data
m_conn.Open();
string sql = "insert into [" + m_tablename + "] values('" + mhtlocation + "')";
m_cmd.CommandText = sql;
m_cmd.ExecuteNonQuery();
m_conn.Close();
}
catch (System.Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
更多關于C#相關內(nèi)容感興趣的讀者可查看本站專題:《C#程序設計之線程使用技巧總結》、《C#操作Excel技巧總結》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數(shù)據(jù)結構與算法教程》、《C#數(shù)組操作技巧總結》及《C#面向?qū)ο蟪绦蛟O計入門教程》
希望本文所述對大家C#程序設計有所幫助。
- C#操作SQLite數(shù)據(jù)庫幫助類詳解
- c#幾種數(shù)據(jù)庫的大數(shù)據(jù)批量插入(SqlServer、Oracle、SQLite和MySql)
- C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法
- C#操作SQLite數(shù)據(jù)庫方法小結(創(chuàng)建,連接,插入,查詢,刪除等)
- C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
- C#基于SQLiteHelper類似SqlHelper類實現(xiàn)存取Sqlite數(shù)據(jù)庫的方法
- C#操作SQLite方法實例詳解
- C#調(diào)用SQLite的方法實例分析
- C#封裝的Sqlite訪問類實例
- C#解決SQlite并發(fā)異常問題的方法(使用讀寫鎖)
- C#事務處理(Execute Transaction)實例解析
- C# SQLite事務操作方法分析
相關文章
Unity輸出帶點擊跳轉功能的Log實現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點擊跳轉功能的Log實現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
算法練習之從String.indexOf的模擬實現(xiàn)開始
這篇文章主要介紹了算法練習從String.indexOf的模擬實現(xiàn)開始,需要的朋友可以參考下2014-12-12
Unity編輯器資源導入處理函數(shù)OnPreprocessAudio用法示例
這篇文章主要為大家介紹了Unity編輯器資源導入處理函數(shù)OnPreprocessAudio用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
詳解C#如何使用重載方法實現(xiàn)不同類型數(shù)據(jù)的計算
這篇文章主要為大家詳細介紹了C#如何使用重載方法實現(xiàn)不同類型數(shù)據(jù)的計算,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-02-02

