SQLite官网:https://www.sqlite.org/index.html 源视频教程:https://www.bilibili.com/video/BV1Zz411i78o 菜鸟教程文档:https://www.runoob.com/sqlite/sqlite-tutorial.html
一、数据库简介与基本语法
1.1-数据库的作用
- txt去保存1万行的数据.(数据量超过一定量级[ 大于1w ])
- 数据格式的管理,以及数据内容的分片
1.2-数据库的选择
- 目前所说:都是SQL(结构化查询语言)语句
- 单机版本:
- ACCESS(微软)
- 最大缺点:必须要安装Office、数据量、查询速度、写法有少许不同
- SQLite
- 唯一携带一个DLL驱动文件(几百K)
- 缺点:超过10w的,不建议使用。
- ACCESS(微软)
- 企业级数据库:
- MsSQLServer
- 数据量:5000w没什么问题
- 最适合C#
- My SQL:
- 要一份非.net官方的驱动
- 开源
- 相对于MSSQL Server,优势是体积小,跨平台
- Oracle:
- 需要非官方驱动
- 适合JAVA
- MongDB:
- 后期支秀
- 非关系型数据库
- MsSQLServer
二、数据库增删改查语法与实例
2.1-创建表
(1)下载并打开这个工具
(2)创建一个数据库,然后创建一个表如下:
(3)添加列明、数据类型、约束
2.2-增删改查
--插入--注意:Integer允许自动增长(不要被Identity 忽悠)insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1001,'admin','admin','2021-01-21')insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1002,'sanha','sanha', datetime('now','localtime'))--查询select * from UserInfo--Limit 跳过几个,取几个--Limit 2,2 跳过2个,取2个--删除delete from UserInfo where UserId=1002--修改update UserInfo set UserNames='sanha_update' where UserId=1002
2.3-使用WinForm和SQLite做登录注册
(1)管理Nuget程序包,下载这个类库:
1.1-将数据库文件拷贝在Bin路径下。

(2)写一个SQLite帮助类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SQLite;using System.Threading.Tasks;using System.Configuration;namespace SQLite{public class SQLiteHelper{private readonly string _str;public SQLiteHelper(string str) {_str = str;}//获取连接字符串//private static readonly string str = ConfigurationManager.ConnectionStrings["DBFilerURL"].ConnectionString;/// <summary>/// 做增删改的功能/// </summary>/// <param name="sql">SQL语句</param>/// <param name="ps">SQL语句中的参数</param>/// <returns>受影响的行数</returns>public int ExecuteNonQuery(string sql, params SQLiteParameter[] ps){//连接数据库using (SQLiteConnection con = new SQLiteConnection(_str)){using (SQLiteCommand cmd = new SQLiteCommand(sql, con)){con.Open();//打开数据库if (ps != null){cmd.Parameters.AddRange(ps);//参数,加集合(ps)}return cmd.ExecuteNonQuery();}}}/// <summary>/// 查询首行首列/// </summary>/// <param name="sql">SQL语句</param>/// <param name="ps">SQL语句的参数</param>/// <returns>返回首行首列object</returns>public object ExecuteScalar(string sql, params SQLiteParameter[] ps){using (SQLiteConnection con = new SQLiteConnection(_str)){using (SQLiteCommand cmd = new SQLiteCommand(sql, con)){con.Open();if (ps != null){cmd.Parameters.AddRange(ps);}return cmd.ExecuteScalar();}}}/// <summary>/// 查询多行/// </summary>/// <param name="sql">SQL语句</param>/// <param name="ps">SQL语句的参数</param>/// <returns>返回多行SQLiteDataReader</returns>public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps){SQLiteConnection con = new SQLiteConnection(_str);using (SQLiteCommand cmd = new SQLiteCommand(sql, con)){if (ps != null){cmd.Parameters.AddRange(ps);}try{con.Open();return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);}catch (Exception ex){con.Close();con.Dispose();throw ex;}}}/// <summary>/// 查询数据表/// </summary>/// <param name="sql">SQL语句</param>/// <param name="ps">SQL语句中的参数</param>/// <returns>返回表DataTable</returns>public DataTable ExecuteTable(string sql, params SQLiteParameter[] ps){DataTable dt = new DataTable();using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, _str)){if (ps != null){sda.SelectCommand.Parameters.AddRange(ps);}sda.Fill(dt);return dt;}}}}
(3)写一个简单的界面
(4)在后端代码中先写上这些代码
//获取数据库路径public static string SQLitePath = AppDomain.CurrentDomain.BaseDirectory + "db/SQLiteDemo1.db";//数据库连接字符串public static string str = string.Format("Data Source={0};Pooling=true;FailIfMissing=false;", SQLitePath);//实例化对象SQLiteHelper SQLite = new SQLiteHelper(str);
(5)【登录】的逻辑
private void button2_Click(object sender, EventArgs e){string name = this.textBox1.Text.ToString();string password = this.textBox2.Text.ToString();//参数化查询string sql = string.Format("select UserId from UserInfo where UserNames=@name and UserPasss=@password;");SQLiteParameter[] parameters =new SQLiteParameter[]{new SQLiteParameter("@name",name),new SQLiteParameter("@password",password)};object obj=SQLite.ExecuteScalar(sql, parameters);int i =Convert.ToInt32(obj);if (i > 0){this.label4.Text = "登录成功!";this.label4.Show();}else {this.label4.Text = "登录失败!";this.label4.Show();}}
(6)【注册】的逻辑
private void button1_Click(object sender, EventArgs e){;string name = this.textBox1.Text.ToString();string password = this.textBox2.Text.ToString();//参数化查询string sql = string.Format("insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(@userid,@username,@passwod,datetime('now','localtime'))");SQLiteParameter[] parameters = new SQLiteParameter[]{new SQLiteParameter("@userid",new Random().Next(10)),new SQLiteParameter("@username",name),new SQLiteParameter("@passwod",password)};object obj = SQLite.ExecuteNonQuery(sql, parameters);int i = Convert.ToInt32(obj);if (i > 0){this.label4.Text = "注册成功!";this.label4.Show();}else{this.label4.Text = "注册失败!";this.label4.Show();}}
