• C#-SQLite-使用教程笔记


    微软官网资料链接(可下载文档)

    教程参考链接:SQLite 教程 - SQLite中文手册

    项目中对应的system.dat文件可以用SQLiteStudio打开查看

    参考文档:https://d7ehk.jb51.net/202008/books/SQLite_jb51.rar

    总结介绍

    1、下载SQLiteStudio对数据库文件进行管理、查看

    2、代码中通过NuGet添加System.Data.SQLite

    3、代码类:SQLiteConnection连接本地数据库文件》SQLiteCommand配置命令并执行》连接关闭

    一、SQLite数据下载,安装,使用(管理查看数据)

    1,下载

    下载绿色免安装版本

    链接:百度网盘 请输入提取码

    提取码:hgoc

    1、参考网站:SQLite Home Page,下载地址:System.Data.SQLite: Downloads Page

    2,安装

      免安装,双击SQLiteStudio可以打开使用,如果放在项目中,建议复制到C:\Program Files (x86) ,防止被误删除掉

    3,添加数据库

      数据库》添加数据库》输入数据名称》确定

    4,创建数据表

      Tables》新建表

     自动生成的创建表的sql语句

    1

    2

    3

    4

    5

    6

    7

    8

    CREATE TABLE SysAdmin (

        

    LoginID   INTEGER      PRIMARY KEY AUTOINCREMENT

                               

    DEFAULT (10000),

        

    LoginName VARCHAR (20) NOT NULL,

        

    LoginPwd  VARCHAR (50) NOT NULL,

        

    Role      INTEGER      DEFAULT (0)

                               

    NOT NULL

    );

      

    5,添加字段

    6,数据库的位置

      数据表都创建好了之后,鼠标放在数据库名上,就显示数据库所在的目录

    二、C#使用SQLite数据需要添加的引用(上面的wiki路径下载里有)

    1,VS使用NuGet添加使用,搜索:System.Data.SQLite添加即可

    三、代码中数据库命令操作

    包含功能:增删数据库文件;增删改数据表;增删改查数据内容

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Data.SQLite;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Threading.Tasks;
    10. using System.Windows.Forms;
    11. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
    12. namespace MyFrom
    13. {
    14. public partial class Form1 : Form
    15. {
    16. public Form1()
    17. {
    18. InitializeComponent();
    19. }
    20. // 添加数据表
    21. private void button1_Click(object sender, EventArgs e)
    22. {
    23. string path = Application.StartupPath + "\\test.db";
    24. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    25. //cn.Open();
    26. cn.Close();
    27. label1.Text = "添加数据库完成";
    28. }
    29. // 删除数据表
    30. private void button2_Click(object sender, EventArgs e)
    31. {
    32. string path = Application.StartupPath + "\\test.db";
    33. if (System.IO.File.Exists(path))
    34. {
    35. System.IO.File.Delete(path);
    36. }
    37. label1.Text = "删除数据库完成";
    38. }
    39. // 添加数据表
    40. private void button3_Click(object sender, EventArgs e)
    41. {
    42. string path = Application.StartupPath + "\\test.db";
    43. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    44. if (cn.State != System.Data.ConnectionState.Open)
    45. {
    46. cn.Open();
    47. SQLiteCommand cmd = new SQLiteCommand();
    48. cmd.Connection = cn;
    49. // cmd.CommandText = "CREATE TABLE t1(time string,id varchar(4),score int)";
    50. cmd.CommandText = "CREATE TABLE IF NOT EXISTS t1(time string,id varchar(4),score int)";
    51. cmd.ExecuteNonQuery();
    52. }
    53. cn.Close();
    54. label1.Text = "添加数据表完成";
    55. }
    56. // 删除数据表
    57. private void button4_Click(object sender, EventArgs e)
    58. {
    59. string path = Application.StartupPath + "\\test.db";
    60. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    61. if (cn.State != System.Data.ConnectionState.Open)
    62. {
    63. cn.Open();
    64. SQLiteCommand cmd = new SQLiteCommand();
    65. cmd.Connection = cn;
    66. cmd.CommandText = "DROP TABLE IF EXISTS t1";
    67. cmd.ExecuteNonQuery();
    68. }
    69. cn.Close();
    70. label1.Text = "删除数据表完成";
    71. }
    72. // 更改数据表名
    73. private void button5_Click(object sender, EventArgs e)
    74. {
    75. string path = Application.StartupPath + "\\test.db";
    76. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    77. if (cn.State != System.Data.ConnectionState.Open)
    78. {
    79. cn.Open();
    80. SQLiteCommand cmd = new SQLiteCommand();
    81. cmd.Connection = cn;
    82. cmd.CommandText = "ALTER TABLE t3 RENAME TO t1";
    83. cmd.ExecuteNonQuery();
    84. }
    85. cn.Close();
    86. label1.Text = "更改表名完成";
    87. }
    88. // 添加数据表列元素
    89. private void button6_Click(object sender, EventArgs e)
    90. {
    91. string path = Application.StartupPath + "\\test.db";
    92. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    93. if (cn.State != System.Data.ConnectionState.Open)
    94. {
    95. cn.Open();
    96. SQLiteCommand cmd = new SQLiteCommand();
    97. cmd.Connection = cn;
    98. cmd.CommandText = "ALTER TABLE t1 ADD COLUMN age int";
    99. cmd.ExecuteNonQuery();
    100. }
    101. cn.Close();
    102. label1.Text = "添加列完成";
    103. }
    104. // 添加数据
    105. private void button7_Click(object sender, EventArgs e)
    106. {
    107. string path = Application.StartupPath + "\\test.db";
    108. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    109. if (cn.State != System.Data.ConnectionState.Open)
    110. {
    111. cn.Open();
    112. SQLiteCommand cmd = new SQLiteCommand();
    113. cmd.Connection = cn;
    114. //time string,id varchar(4),score int
    115. cmd.CommandText = "INSERT INTO t1(time,id,score) VALUES(@time,@id,@score)";
    116. cmd.Parameters.Add("id", DbType.String).Value = "666";
    117. //cmd.Parameters.Add("age", DbType.Int32).Value = n;
    118. cmd.Parameters.Add("score", DbType.Int32).Value = 22;
    119. cmd.Parameters.Add("time", DbType.String).Value = DateTime.Now.ToString();
    120. cmd.ExecuteNonQuery();
    121. }
    122. cn.Close();
    123. label1.Text = "添加数据完成";
    124. }
    125. // 更改数据
    126. private void button8_Click(object sender, EventArgs e)
    127. {
    128. string s = "888";
    129. int n = 1077777;
    130. int myscore = 1;
    131. string path = Application.StartupPath + "\\test.db";
    132. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    133. if (cn.State != System.Data.ConnectionState.Open)
    134. {
    135. cn.Open();
    136. SQLiteCommand cmd = new SQLiteCommand();
    137. cmd.Connection = cn;
    138. //time string,id varchar(4),score int
    139. cmd.CommandText = "UPDATE t1 SET id=@id,age=@age WHERE id='666'";
    140. cmd.Parameters.Add("id", DbType.String).Value = s;
    141. cmd.Parameters.Add("age", DbType.Int32).Value = n;
    142. cmd.ExecuteNonQuery();
    143. }
    144. cn.Close();
    145. label1.Text = "更改数据完成";
    146. }
    147. // 删除数据
    148. private void button9_Click(object sender, EventArgs e)
    149. {
    150. string path = Application.StartupPath + "\\test.db";
    151. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    152. if (cn.State != System.Data.ConnectionState.Open)
    153. {
    154. cn.Open();
    155. SQLiteCommand cmd = new SQLiteCommand();
    156. cmd.Connection = cn;
    157. //time string,id varchar(4),score int
    158. cmd.CommandText = "DELETE FROM t1 WHERE id='888'";
    159. cmd.ExecuteNonQuery();
    160. }
    161. cn.Close();
    162. label1.Text = "删除数据完成";
    163. }
    164. // 查询数据
    165. private void button10_Click(object sender, EventArgs e)
    166. {
    167. string path = Application.StartupPath + "\\test.db";
    168. SQLiteConnection cn = new SQLiteConnection("data source=" + path);
    169. if (cn.State != System.Data.ConnectionState.Open)
    170. {
    171. cn.Open();
    172. SQLiteCommand cmd = new SQLiteCommand();
    173. cmd.Connection = cn;
    174. //time string,id varchar(4),score int
    175. cmd.CommandText = "SELECT * FROM t1 WHERE rowid=2"; // 读取第二行,行数从1开始
    176. SQLiteDataReader sr = cmd.ExecuteReader();
    177. Console.WriteLine("查询到的数据如下:");
    178. while (sr.Read())
    179. {
    180. int count = sr.VisibleFieldCount;
    181. for (int i = 0; i < count; i++)
    182. {
    183. Console.WriteLine(sr[i].ToString() + " ");
    184. }
    185. string s = sr.GetString(0);
    186. Console.WriteLine(s);
    187. }
    188. sr.Close();
    189. }
    190. cn.Close();
    191. label1.Text = "查询数据完成";
    192. }
    193. }
    194. }

  • 相关阅读:
    【二】2D测量 Metrology——read_metrology_model()算子
    【深度学习】深入解码:提升NLP生成文本的策略与参数详解
    MYSQL(事务)
    SpringBoot之yaml语法、配置文件、多环境切换
    Day38—— 509. 斐波那契数 70. 爬楼梯 746. 使用最小花费爬楼梯 (动态规划)
    tez作业运行慢
    Rust 登上了开源头条「GitHub 热点速览」
    【自然语言处理(NLP)】基于PaddleNLP的短文本相似度计算
    (尚硅谷)JavaWeb新版教程11-Cookie-Kaptcha-Exp
    记录一次关于Rank()排序函数问题
  • 原文地址:https://blog.csdn.net/qq_17242837/article/details/132678183