使用sqlSugar和仓储模式实现增删改查




1、Student类,数据库实体

public class Student
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Name { get; set; }
public string address { get; set; }
}
2、ConnectionConfiguration类,数据库连接

public class ConnectionConfiguration
{
public static string Connection { get; set; } = @"Data Source=xxx;Initial Catalog=Study;Integrated Security=True";
}
3、Repository类,定义仓储

public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository(ISqlSugarClient context = null) : base(context)
{
if (context == null)
{
base.Context = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = ConnectionConfiguration.Connection
});
}
}
}
4、StudentService类,服务类

public class StudentService : Repository<Student>
{
public List<Student> GetStudents()
{
return base.GetList();
}
public void Insert(Student student)
{
base.Insert(student);
}
public void Update(Student student)
{
base.Update(student);
}
public void Delete(Student student)
{
base.Delete(student);
}
}
5、页面

public partial class Form1 : Form
{
StudentService studentService;
public Form1()
{
InitializeComponent();
studentService = new StudentService();
Load();
}
/// <summary>
/// 加载数据
/// </summary>
void Load()
{
dataGridView1.Rows.Clear();
List<Student> ss = studentService.GetStudents();
foreach (var student in ss)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[0].Value = student.Id;
dataGridView1.Rows[index].Cells[1].Value = student.Name;
dataGridView1.Rows[index].Cells[2].Value = student.address;
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Load();
}
/// <summary>
/// 插入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
int index = dataGridView1.CurrentRow.Index;
if (index != -1)
{
Student student = new Student();
student.Name = dataGridView1.Rows[index].Cells[1].Value.ToString();
student.address = dataGridView1.Rows[index].Cells[2].Value.ToString();
studentService.Insert(student);
Load();
MessageBox.Show("插入成功");
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
int index = dataGridView1.CurrentRow.Index;
if (index != -1)
{
Student student = new Student();
student.Id = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
student.Name = dataGridView1.Rows[index].Cells[1].Value.ToString();
student.address = dataGridView1.Rows[index].Cells[2].Value.ToString();
studentService.Update(student);
MessageBox.Show("更新成功");
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
try
{
int index = dataGridView1.CurrentRow.Index;
if (index != -1)
{
Student student = new Student();
student.Id = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
studentService.Delete(student);
Load();
MessageBox.Show("删除成功");
}
}
catch (Exception ex)
{
}
}
}