查询 Sql Server 指定的数据表,并显示自己需要的记录。是非常常用的。小白该如何 *** 作呢?
>Imports System
Imports SystemDataSqlClient
Imports SystemConfiguration
Imports SystemCollectionsGeneric
Imports SystemText
Imports SystemData
Imports SystemConfigurationConfigurationSettings
Imports SystemDataOleDb
Public Class DB
Private Shared com As OleDbOleDbCommand
Private Shared reader As OleDbOleDbDataReader
Private Shared adapter As OleDbOleDbDataAdapter
Private Shared conn As OleDbOleDbConnection
''' <summary>
''' 获取数据库连接
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Shared ReadOnly Property NewConn() As OleDbConnection
Get
Dim connectionString As String
'connectionString = SystemConfigurationConfigurationSettingsGetConfig("Supermarket")
'连接2010数据库
'connectionString = "Provider=MicrosoftACEOLEDB120;Data Source=F:\GCUaccdb"
'连接03-07数据库
connectionString = "Provider=MicrosoftJetOLEDB40;Data Source=F:\JL\GCUMDB"
'应该在这里先判断conn是否为Nothing
If conn Is Nothing Then
conn = New OleDbOleDbConnection(connectionString)
End If
If connState <> ConnectionStateOpen Then
connOpen()
End If
Return conn
End Get
End Property
''' <summary>
''' 执行增删改(无参)
''' </summary>
''' <param name="sql">执行的Sql语句</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ExecuteNonQuery(ByVal sql As String) As Integer
com = New OleDbOleDbCommand(sql, NewConn())
Return comExecuteNonQuery()
End Function
''' <summary>
''' 执行增删改(有参)
''' </summary>
''' <param name="sql"></param>
''' <param name="para"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ExecuteNonQuery(ByVal sql As String, ByVal para As OleDbParameter()) As Integer
com = New OleDbOleDbCommand(sql, NewConn)
comParametersAddRange(para)
Return comExecuteNonQuery()
End Function
''' <summary>
''' 执行增删改的存储过程
''' </summary>
''' <param name="para"></param>
''' <param name="ProcedureName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ExecuteNonQuery(ByVal para As SqlParameter(), ByVal ProcedureName As String) As Integer
Dim cmd As OleDbOleDbCommand
cmd = New OleDbOleDbCommand()
cmdConnection = NewConn()
cmdCommandText = ProcedureName
cmdCommandType = CommandTypeStoredProcedure
cmdParametersAddRange(para)
Return comExecuteNonQuery
End Function
''' <summary>
''' 执行查询(返回一个结果集,无参)
''' </summary>
''' <param name="sql"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetScalar(ByVal sql As String) As String
Dim dataset As DataSet
dataset = New DataSet()
com = New OleDbOleDbCommand(sql, NewConn)
adapter = New OleDbDataAdapter(com)
adapterFill(dataset)
If datasetTablesCount > 0 And datasetTables(0)RowsCount > 0 Then
Return datasetTables(0)Rows(0)(0)ToString()
End If
Return "Null"
End Function
''' <summary>
''' 执行查询(返回一个结果集,有参)
''' </summary>
''' <param name="sql"></param>
''' <param name="para"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetScalar(ByVal sql As String, ByVal para As SqlParameter()) As Integer
com = New OleDbOleDbCommand(sql, NewConn)
comParametersAddRange(para)
Return ConvertToInt32(comExecuteScalar())
End Function
''' <summary>
''' 执行查询(返回一行数据,无参)
''' </summary>
''' <param name="sql"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetReader(ByVal sql As String) As OleDbDataReader
com = New OleDbOleDbCommand(sql, NewConn)
reader = comExecuteReader()
Return reader
End Function
''' <summary>
''' 执行查询(返回一行数据,有参)
''' </summary>
''' <param name="sql"></param>
''' <param name="para"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetReader(ByVal sql As String, ByVal para As SqlParameter()) As OleDbDataReader
com = New OleDbOleDbCommand(sql, NewConn)
comParametersAddRange(para)
reader = comExecuteReader()
Return reader
End Function
''' <summary>
''' 执行查询(返回一个数据集,无参)
''' </summary>
''' <param name="sql"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetDataSet(ByVal sql As String) As DataTable
Dim dataset As DataSet
dataset = New DataSet()
com = New OleDbOleDbCommand(sql, NewConn)
adapter = New OleDbDataAdapter(com)
adapterFill(dataset)
Return datasetTables(0)
End Function
''' <summary>
''' 执行查询(返回一个数据集,有参)
''' </summary>
''' <param name="sql"></param>
''' <param name="para"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetDataSet(ByVal sql As String, ByVal para As SqlParameter()) As DataTable
Dim dataset As DataSet
dataset = New DataSet()
com = New OleDbCommand(sql, NewConn)
comParametersAddRange(para)
adapter = New OleDbDataAdapter(com)
adapterFill(dataset)
Return datasetTables(0)
End Function
End Class
以上就是关于如何使用SqlServer查询数据库表所有或指定记录全部的内容,包括:如何使用SqlServer查询数据库表所有或指定记录、谁可以提供vb写的数据库 *** 作类,sqlserver的,包括查询,增删改等 *** 作,越详细越要,万分感谢、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)