VB.net 简单的SQL封装

僤鯓⒐⒋嵵緔 提交于 2020-03-04 15:17:18
Imports System.Data.SqlClient
Public Module sql
    Public sqls As SqlConnection
    Public cmd As SqlCommand
    Public Sub lianjie()
        sqls = New SqlConnection("Data Source=ip地址;Initial Catalog=数据库名字;Persist Security Info=True;User ID=登录名;Password=密码")
        sqls.Open()
        cmd = New SqlCommand()
        cmd.Connection = sqls
    End Sub
    Public Function 执行sql函数(函数名 As String, ParamArray 参数() As String)
        cmd.CommandText = $"DECLARE @a int;exec @a={函数名} {Strings.Join(参数, ",")};select @a"
        Return cmd.ExecuteScalar
    End Function

    Public Function 执行sql查询(sqltext As String) As List(Of List(Of String))
        cmd.CommandText = sqltext
        Dim ret = cmd.ExecuteReader
        Dim t As New List(Of List(Of String))
        While ret.Read
            Dim hang As New List(Of String)
            For i = 0 To ret.FieldCount - 1
                hang.Add(ret(i))
            Next
            t.Add(hang)
        End While
        ret.Close()
        Return t
    End Function

    Public Function 执行sql语句_返回行数(sqltext As String)
        cmd.CommandText = sqltext
        Return cmd.ExecuteNonQuery
    End Function
    Public Function 执行sql语句_返回第一个结果(sqltext As String)
        cmd.CommandText = sqltext
        Return cmd.ExecuteScalar
    End Function
End Module

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!