初次接触SQLite数据库,要用到WinForm程序编程的话,需要先下载System.Data.SQLite库。网址在此:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
我最后下载的版本是sqlite-netFx40-binary-bundle-Win32-2010-1.0.94.0.zip,
后面看了看,应该下载sqlite-netFx40-binary-bundle-x64-2010-1.0.112.0.zip,不过现在下的这个也能用。
有的版本带bundle,有的不带bundle,区别好像就是有没有SQLite.Interop.dll这个库。带bundle的是直接把这个库包含在内了。【可能说的不太对?】
下好了之后在VS里添加引用,
再在代码开头加上using System.Data.SQLite;就可以用了
SQLitehelper.cs跟SQL Server的类似,不过是把里面的SQLxxx换成了SQLitexxx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SQLite;
namespace AAA
{
class sqlitehelper
{
public static SQLiteConnection My_Conn;
//Data Source是数据库的地址,Version是数据库的版本
public static string connectionstr = string.Format("Data Source=XXX.db;Version=3");
public static SQLiteConnection getcon()
{
My_Conn = new SQLiteConnection(connectionstr);
My_Conn.Open();
return My_Conn;
}
public void con_open()
{
getcon();
}
public void conn_close()
{
if (My_Conn.State == ConnectionState.Open)
{
My_Conn.Close();
My_Conn.Dispose();
}
}
public SQLiteDataReader getsdr(string sqlstr)
{
getcon();
SQLiteCommand My_com = My_Conn.CreateCommand();
My_com.CommandText = sqlstr;
SQLiteDataReader My_Reader = My_com.ExecuteReader();
return My_Reader;
}
public void dosqlcom(string sqlstr)
{
getcon();
SQLiteCommand sqlcom = new SQLiteCommand(sqlstr, My_Conn);
sqlcom.ExecuteNonQuery();
sqlcom.Dispose();
conn_close();
}
public DataSet getDs(string sqlstr, string tableName)
{
getcon();
SQLiteDataAdapter sqlda = new SQLiteDataAdapter(sqlstr, My_Conn);
DataSet My_DataSet = new DataSet();
sqlda.Fill(My_DataSet, tableName);
conn_close();
return My_DataSet;
}
}
}
使用的话,也是跟sqlhelper类似,只是把类型换成SQLitexxx,就不赘述了。