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;
}
}
}
}