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的,不建议使用。
- 企业级数据库:
- MsSQLServer
- My SQL:
- 要一份非.net官方的驱动
- 开源
- 相对于MSSQL Server,优势是体积小,跨平台
- Oracle:
- MongDB:
二、数据库增删改查语法与实例
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帮助类(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();
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |