首页 技术 正文
技术 2022年11月9日
0 收藏 884 点赞 2,714 浏览 1830 个字

SQLite,一种轻量级的数据库

想要使用的话首先下载安装包。

https://www.sqlite.org/download.html

下载sqlite-netFx20-setup-bundle-x86-2005-1.0.102.0.exe后安装

默认.dll文件所在路径为C:\Program Files (x86)\System.Data.SQLite\2005\bin\System.Data.SQLite.dll

打开VS,新建项目,引用System.Data.SQLite.dll 编写基本操作数据库的类。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;namespace Test
{
public class SQLHelper
{
private SQLiteConnection conn = null; private static SQLHelper _Instance = null;
public static SQLHelper Instance
{
get
{
if (_Instance == null)
{
_Instance = new SQLHelper();
}
return _Instance;
}
}
private SQLHelper()
{
string dbPath = "Data Source =" + Environment.CurrentDirectory + "/face.db"; //创建数据库实例,指定文件位置
conn = new SQLiteConnection(dbPath); //打开数据库,若文件不存在会自动创建
conn.Open();
} ~SQLHelper()
{
try
{
conn.Close();
conn.Dispose();
}
catch
{
}
_Instance = null;
} public int ExecuteNonQuery(string sqlstr,params SQLiteParameter[] praam)
{
SQLiteCommand cmdInsert = new SQLiteCommand(sqlstr, conn); foreach (SQLiteParameter p in praam)
{
cmdInsert.Parameters.Add(p);
} return cmdInsert.ExecuteNonQuery();
} public DataSet Query(string sqlstr)
{
DataSet ds = new DataSet();
using (SQLiteCommand cmd = new SQLiteCommand(sqlstr,this.conn))
{
using (SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd))
{
sda.Fill(ds);
}
}
return ds;
} }
}

创建数据库结构可以通过语句在程序中建立也可以使用其它可视化工具进行。

下载了SQLiteStudio感觉使用起来还不错、软件还自带简体中文(选择之后程序界面一半中文一半英文,反而感觉奇怪)

建好一张表后使用程序进行insert和select测试,一切顺利。

List<SQLiteParameter> list = new List<SQLiteParameter>();
list.Add(new SQLiteParameter("@Id", md5));
list.Add(new SQLiteParameter("@Name", "TEST"));
list.Add(new SQLiteParameter("@Code", "AABBCC"));
list.Add(new SQLiteParameter("@Size", ));
list.Add(new SQLiteParameter("@Text", ""));
list.Add(new SQLiteParameter("@AddDate", DateTime.Now));
SQLHelper.Instance.ExecuteNonQuery("insert into info values(@Id,@Name,@Code,@Size,@Text,@AddDate)", list.ToArray());DataTable Info = SQLHelper.Instance.Query("select * from Info").Tables[];
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901