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
      • 数据量:5000w没什么问题
      • 最适合C#
    • My SQL:
      • 要一份非.net官方的驱动
      • 开源
      • 相对于MSSQL Server,优势是体积小,跨平台
    • Oracle:
      • 需要非官方驱动
      • 适合JAVA
    • MongDB:
      • 后期支秀
      • 非关系型数据库

二、数据库增删改查语法与实例

2.1-创建表

(1)下载并打开这个工具
image.png
(2)创建一个数据库,然后创建一个表如下:
image.png
(3)添加列明、数据类型、约束
image.png

2.2-增删改查

  1. --插入
  2. --注意:Integer允许自动增长(不要被Identity 忽悠)
  3. insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1001,'admin','admin','2021-01-21')
  4. insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(1002,'sanha','sanha', datetime('now','localtime'))
  5. --查询
  6. select * from UserInfo
  7. --Limit 跳过几个,取几个
  8. --Limit 2,2 跳过2个,取2
  9. --删除
  10. delete from UserInfo where UserId=1002
  11. --修改
  12. update UserInfo set UserNames='sanha_update' where UserId=1002

2.3-使用WinForm和SQLite做登录注册

(1)管理Nuget程序包,下载这个类库:
image.png
1.1-将数据库文件拷贝在Bin路径下。
image.png
image.png
(2)写一个SQLite帮助类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SQLite;
  7. using System.Threading.Tasks;
  8. using System.Configuration;
  9. namespace SQLite
  10. {
  11. public class SQLiteHelper
  12. {
  13. private readonly string _str;
  14. public SQLiteHelper(string str) {
  15. _str = str;
  16. }
  17. //获取连接字符串
  18. //private static readonly string str = ConfigurationManager.ConnectionStrings["DBFilerURL"].ConnectionString;
  19. /// <summary>
  20. /// 做增删改的功能
  21. /// </summary>
  22. /// <param name="sql">SQL语句</param>
  23. /// <param name="ps">SQL语句中的参数</param>
  24. /// <returns>受影响的行数</returns>
  25. public int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
  26. {
  27. //连接数据库
  28. using (SQLiteConnection con = new SQLiteConnection(_str))
  29. {
  30. using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
  31. {
  32. con.Open();//打开数据库
  33. if (ps != null)
  34. {
  35. cmd.Parameters.AddRange(ps);//参数,加集合(ps)
  36. }
  37. return cmd.ExecuteNonQuery();
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// 查询首行首列
  43. /// </summary>
  44. /// <param name="sql">SQL语句</param>
  45. /// <param name="ps">SQL语句的参数</param>
  46. /// <returns>返回首行首列object</returns>
  47. public object ExecuteScalar(string sql, params SQLiteParameter[] ps)
  48. {
  49. using (SQLiteConnection con = new SQLiteConnection(_str))
  50. {
  51. using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
  52. {
  53. con.Open();
  54. if (ps != null)
  55. {
  56. cmd.Parameters.AddRange(ps);
  57. }
  58. return cmd.ExecuteScalar();
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// 查询多行
  64. /// </summary>
  65. /// <param name="sql">SQL语句</param>
  66. /// <param name="ps">SQL语句的参数</param>
  67. /// <returns>返回多行SQLiteDataReader</returns>
  68. public SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
  69. {
  70. SQLiteConnection con = new SQLiteConnection(_str);
  71. using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
  72. {
  73. if (ps != null)
  74. {
  75. cmd.Parameters.AddRange(ps);
  76. }
  77. try
  78. {
  79. con.Open();
  80. return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
  81. }
  82. catch (Exception ex)
  83. {
  84. con.Close();
  85. con.Dispose();
  86. throw ex;
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// 查询数据表
  92. /// </summary>
  93. /// <param name="sql">SQL语句</param>
  94. /// <param name="ps">SQL语句中的参数</param>
  95. /// <returns>返回表DataTable</returns>
  96. public DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
  97. {
  98. DataTable dt = new DataTable();
  99. using (SQLiteDataAdapter sda = new SQLiteDataAdapter(sql, _str))
  100. {
  101. if (ps != null)
  102. {
  103. sda.SelectCommand.Parameters.AddRange(ps);
  104. }
  105. sda.Fill(dt);
  106. return dt;
  107. }
  108. }
  109. }
  110. }

(3)写一个简单的界面
image.png
(4)在后端代码中先写上这些代码

  1. //获取数据库路径
  2. public static string SQLitePath = AppDomain.CurrentDomain.BaseDirectory + "db/SQLiteDemo1.db";
  3. //数据库连接字符串
  4. public static string str = string.Format("Data Source={0};Pooling=true;FailIfMissing=false;", SQLitePath);
  5. //实例化对象
  6. SQLiteHelper SQLite = new SQLiteHelper(str);

(5)【登录】的逻辑

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. string name = this.textBox1.Text.ToString();
  4. string password = this.textBox2.Text.ToString();
  5. //参数化查询
  6. string sql = string.Format("select UserId from UserInfo where UserNames=@name and UserPasss=@password;");
  7. SQLiteParameter[] parameters =new SQLiteParameter[]
  8. {
  9. new SQLiteParameter("@name",name),
  10. new SQLiteParameter("@password",password)
  11. };
  12. object obj=SQLite.ExecuteScalar(sql, parameters);
  13. int i =Convert.ToInt32(obj);
  14. if (i > 0)
  15. {
  16. this.label4.Text = "登录成功!";
  17. this.label4.Show();
  18. }
  19. else {
  20. this.label4.Text = "登录失败!";
  21. this.label4.Show();
  22. }
  23. }

(6)【注册】的逻辑

  1. private void button1_Click(object sender, EventArgs e)
  2. {;
  3. string name = this.textBox1.Text.ToString();
  4. string password = this.textBox2.Text.ToString();
  5. //参数化查询
  6. string sql = string.Format("insert into UserInfo(UserId,UserNames,UserPasss,RegDate) values(@userid,@username,@passwod,datetime('now','localtime'))");
  7. SQLiteParameter[] parameters = new SQLiteParameter[]
  8. {
  9. new SQLiteParameter("@userid",new Random().Next(10)),
  10. new SQLiteParameter("@username",name),
  11. new SQLiteParameter("@passwod",password)
  12. };
  13. object obj = SQLite.ExecuteNonQuery(sql, parameters);
  14. int i = Convert.ToInt32(obj);
  15. if (i > 0)
  16. {
  17. this.label4.Text = "注册成功!";
  18. this.label4.Show();
  19. }
  20. else
  21. {
  22. this.label4.Text = "注册失败!";
  23. this.label4.Show();
  24. }
  25. }