使用Dapper创建一个简单的查询

1.先在NuGet上下载Dapper包

2.创建对应的model

代码如下:

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 数据显示
{
    public class User
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }

    }
}

3.添加如下的配置文件

4.在配置文件中添加对应的connectionStrings

5.设计简单的页面(当点击查询的时候数据会显示出来)

6.后端代码如下:

cs 复制代码
using Dapper;
using Microsoft.Data.SqlClient;
using System.Configuration;

namespace 数据显示
{
    public partial class Form1 : Form
    {
        string conn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            btnFind.Click += BtnFind_Click;
        }

        private void BtnFind_Click(object? sender, EventArgs e)
        {
            using (var db=new SqlConnection(conn))
            {
                string sql = "select * from Users";
                var list=db.Query<User>(sql);
                dataGridView.DataSource = list;
                dataGridView.Columns["UserId"].Visible = false;
            } 
           
        }
    }
}

数据的表如下:

相关推荐
何以解忧唯有撸码15 小时前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手1 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库2 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc2 天前
C# 发展历史
开发语言·c#
kyle~2 天前
C# --- 反射
开发语言·c#
暴走约伯3 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316344 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船4 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261355 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船5 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug