在VB.NET中使用SQLSUGAR
本文使用SqlSugarScope 单例模式,新建一个VB.NET的窗体项目,添加NuGet程序包
搜索SQLSUGAR然后在右侧点击安装,可以根据项目的.NET版本选择SQLSUGAR的版本。
在Form1中添加三个按钮,把如下程序复制进程序。
需要注意,在SQLSever中要创建一个名为Student的表,表格式要跟Class Student中的配置保持一致。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
Imports SqlSugar Public Class Form1 ''' <summary> ''' 插入 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim list = SqlSugarHelper.Db.Queryable(Of Student)().ToList() SqlSugarHelper.Db.Insertable(New Student With {.SchoolId = 1, .Name = "jack"}).ExecuteCommand() End Sub ''' <summary> ''' 更新 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click SqlSugarHelper.Db.Updateable(New Student With {.Id = 1, .SchoolId = 2, .Name = "jack2"}).ExecuteCommand() End Sub ''' <summary> ''' 删除 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click SqlSugarHelper.Db.Deleteable(Of Student)().Where(Function(it) it.Id = 1).ExecuteCommand() End Sub End Class Public Class Student <SugarColumn(IsPrimaryKey:=True, IsIdentity:=True)> Public Property Id As Integer Public Property SchoolId As Integer? Public Property Name As String End Class Public Class SqlSugarHelper Public Shared Db As SqlSugarScope = New SqlSugarScope(New ConnectionConfig() With { .ConnectionString = "Data Source=SQLEXPRESS; Initial Catalog=King; User Id=sa; Password=123;", .DbType = DbType.SqlServer, .IsAutoCloseConnection = True }, Function(db) db.Aop.OnLogExecuting = Function(sql, pars) Console.WriteLine(sql) End Function End Function) End Class |
坐等大佬D盘更新,祝大佬全家福寿安康
有公众号之类的吗,之前这个网站打不开了
[…] 关于使用Change…
[…] 关于使用Functi…
谢谢提醒,已删 :)