本人是根据自己的经历,将有基于VB.Net的数据库的操作整理如下,或许不是很完整,后面有机会再补充。现将代码里面的大概内容叙述一下:
New(connectstr)方法是创建带参数的SQLDataBase的实例;
UpdateSQL(updatecmd)方法是执行updatecmd数据库语句,更新数据库;
ExecuteSQLToArray(CommandText)函数是通过Sqlreader来获取服务器数据库里面的值,返回的是二维字符串类型的数组;
ExecuteSQLToDataTable(SelectCommandText)函数通过SqlDataAdapter来获取服务器数据库里面的值,返回的是DataTable对象,方便后期的处理;
close()方法是释放资源,关闭数据库连接。
'********************************
' Function: 定义SQL数据库操作的类
' Author: 要点理想色彩
' Createtime: 2018/07https://files.jxasp.com/image/23
' Remark:
'*******************************
Imports System.Data.SqlClient
Public Class SQLDataBase
' 数据库连接字符串
Private cnstr As String = ""
' 数据库执行的SQL脚本
Private sqlstr As String = ""
' 数据库连接对象
Private cn As SqlConnection
' SQl执行语句命令
Private cmd As SqlCommand
''' <summary>
''' 创建SQlDataBase类的实例,并打开以《connectstr》为连接字符串的数据库
''' </summary>
''' <param name="connectstr">连接数据库的字符串;例如:Data Source=IPadress;Initial Catalog=DataBaseName;Integrated Security=false;User ID=用户名;Password=密码;</param>
''' <remarks></remarks>
Public Sub New(ByVal connectstr As String)
' 给数据库连接字符赋值
If connectstr.Length = 0 Then
MsgBox("connectstr为空,创建SQLDataBase失败")
Exit Sub
Else
cnstr = connectstr
End If
' 连接到数据库,并打开
Try
cn = New SqlConnection(cnstr)
cn.Open()
Catch ex As Exception
MsgBox("错误代码为:" & Err.Number & vbCrLf & "错误描述为: " & Err.Description)
Exit Sub
End Try
End Sub
''' <summary>
''' 执行SQL更新语句
''' </summary>
''' <param name="updatecmd"></param>
''' <remarks></remarks>
Public Sub UpdateSQL(ByVal updatecmd As String)
' 给数据库执行脚本sqlstr赋值
If updatecmd.Length = 0 Then
MsgBox("updatecmd为空,无法更新数据库")
Exit Sub
Else
sqlstr = updatecmd
End If
' 给命令对象赋值
cmd = New SqlCommand With {.CommandText = updatecmd, .Connection = cn}
' 定义执行SQL语句收影响的行数
Dim influnrows As Integer
Try
influnrows = cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("错误代码为:" & Err.Number & vbCrLf & "错误描述为: " & Err.Description)
Exit Sub
End Try
End Sub
''' <summary>
''' 通过Sqlreader来获取服务器数据库里面的值,返回的是二维字符串类型的数组
''' </summary>
''' <param name="CommandText">Transact-SQL语句</param>
''' <returns>返回的是二维字符串类型的数组</returns>
''' <remarks></remarks>
Public Function ExecuteSQLToArray(ByVal CommandText As String) As String(,)
' 定义函数的返回结果
Dim FunRet As String(,) = Nothing
' 定义数据库reader对象
Dim reader As SqlDataReader = Nothing
' 给数据库执行脚本sqlstr赋值
If CommandText.Length = 0 Then
MsgBox("updatecmd为空,无法更新数据库")
Return FunRet
Else
sqlstr = CommandText
End If
' 给命令对象赋值
cmd = New SqlCommand With {.CommandText = CommandText, .Connection = cn}
' 给reader对象赋值
reader = cmd.ExecuteReader()
'判断是否有结果
If reader.HasRows = False Then
Return FunRet
End If
'*********************
'remarks:因为reader是一条一条语句的读取, reader只能获取列数, 不能获取行数, 因此需要将总的记录除以列数皆可以获取行数
'*********************
' 定义函数返回数组的列数和行数
Dim lstReader As New List(Of String)
Dim intColumnCount As Integer
Dim intRowsCount As Integer
'将结果集存到listReader列表
intColumnCount = reader.FieldCount
While reader.Read()
For i = 0 To intColumnCount - 1
lstReader.Add(reader.GetValue(i).ToString) '//如果数据库里面是null,则返回的是空值
Next
End While
'获取结果集的行数
intRowsCount = lstReader.Count / intColumnCount
'将结果转化为二位数组
ReDim FunRet(intRowsCount - 1, intColumnCount - 1)
Dim index As Integer = 0
For j = 0 To UBound(FunRet, 1)
For i = 0 To UBound(FunRet, 2)
FunRet(j, i) = lstReader.Item(index)
index = index + 1
Next
Next
'将函数的结果返回
Return FunRet
End Function
''' <summary>
''' 通过SqlDataAdapter来获取服务器数据库里面的值,返回的是DataTable对象
''' </summary>
''' <param name="SelectCommandText">Transact-SQL语句</param>
''' <returns>返回的是二维字符串类型的数组</returns>
''' <remarks></remarks>
Public Function ExecuteSQLToDataTable(ByVal SelectCommandText As String) As DataTable
' 定义函数的返回结果
Dim FunRet As New DataTable
' 给数据库执行脚本sqlstr赋值
If SelectCommandText.Length = 0 Then
MsgBox("updatecmd为空,无法更新数据库")
Return FunRet
End If
'将结果填充到SqlDataAdapter中
Dim sda As SqlDataAdapter
sda = New SqlDataAdapter(SelectCommandText, cn)
sda.Fill(FunRet)
'返回函数的值
Return FunRet
End Function
''' <summary>
''' 释放资源,关闭数据库连接
''' </summary>
''' <remarks></remarks>
Public Sub close()
'关闭cmd对象
If IsNothing(cmd) = False Then
cmd = Nothing
End If
'断开和数据库的连接
If IsNothing(cn) = False Then
cn.Close()
End If
End Sub
End Class