在VB.NET中使用SQLSUGAR

本文使用SqlSugarScope 单例模式,新建一个VB.NET的窗体项目,添加NuGet程序包

搜索SQLSUGAR然后在右侧点击安装,可以根据项目的.NET版本选择SQLSUGAR的版本。

在Form1中添加三个按钮,把如下程序复制进程序。
需要注意,在SQLSever中要创建一个名为Student的表,表格式要跟Class Student中的配置保持一致。

Imports SqlSugar
 
Public Class Form1
    ''' 
    ''' 插入
    ''' 
    ''' 
    ''' 
    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
    ''' 
    ''' 更新
    ''' 
    ''' 
    ''' 
    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
    ''' 
    ''' 删除
    ''' 
    ''' 
    ''' 
    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
    
    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

版权声明:本文内容来源于网络搜集无法获知原创作者,仅供个人学习用途,若侵犯到您的权益请联系我们及时删除。邮箱:1370723259@qq.com

发表评论