winform中使用SqlSugar和SQLite

winform虽然是老古董了,但是在开发桌面方面,还是得心应手的,比如开发一个小工具,小demo之类的。接下来,我们使用SqlSugar和SQLite数据库,在winform中运用。

1.首先建立一个程序

2.安装 System.Data.SQLite,版本1.0.116

3.安装SqlSugar,版本5.0.0

4.增加SqlSugarHelper,关联1.db数据库

数据库创建:

1.我们可以使用Navicat,直接创建数据库

2.随便向表中增加一些数据 

别忘记把1.db复制到bin文件夹下面了

 然后下面关联上1.db数据库

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApp1
{
    public class SqlSugarHelper
    {
        static string url1 = AppDomain.CurrentDomain.BaseDirectory;
        // public static string ConnectionString = ; //必填, 数据库连接字符串
        public static SqlSugarClient db
        {
            get => new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = $"Data Source={url1}/1.db",
                DbType = SqlSugar.DbType.Sqlite,         //必填, 数据库类型
                IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                InitKeyType = InitKeyType.SystemTable    //默认SystemTable, codefist需要使用Attribute
            });
        }
    }
}

5.在界面上面放一个按钮,在按钮下面写代码

6.代码,使用sql语句查询

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = SqlSugarHelper.db.Ado.GetDataTable("SELECT * FROM a");
        }
    }
}

7.使用实体类查询,首先要建立一个实体类

8.使用实体类查询

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = SqlSugarHelper.db.Ado.GetDataTable("SELECT * FROM a");

            var a = SqlSugarHelper.db.Queryable<a>().ToList();
        }
    }
}

9.效果

可见SqlSugar的,既能使用sql语句,还能使用实体类查询,想用什么就用什么,非常的方便自由。

来源:winform中使用SqlSugar和SQLite_winform使用sqlsugar_故里2130的博客-CSDN博客

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值