重新上手C#(二):WinForm程序连接SQLite数据库

初次接触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,就不赘述了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值