赞
踩
目录
(1)下载并打开这个工具
(2)创建一个数据库,然后创建一个表如下:
(3)添加列明、数据类型、约束
- --插入
- --注意: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
(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();
- }
- }
版权声明:本文为原创文章,版权归 [西瓜程序猿] 所有,转载请注明出处,有任何疑问请私信咨询。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。