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
来源:CSDN
作者:aa326358942
链接:https://blog.csdn.net/aa326358942/article/details/104651386