VB.net 对MSSQL操作 查、删、改 三个常规操作

首先,需引用

Imports System.Data.sqlClient.sqlException
Imports System.Data.sqlClient

定义全局变量
Public pubConnection As New sqlConnection
    Public privConDbOther As New sqlConnection
    Public pubsqlCommand As sqlCommand = New sqlCommand
    Public ServerIP As String = "."                 '服务器地址
    Public ServerName As String = ""              '服务器用户名
    Public ServerPassword As String = ""            '服务器密码
    Public DatabaseName As String = ""          '数据库
服务器地址、服务器用户名、服务器密码、数据库 根据你实际情况进行付值


生成数据库连接字符串

Public Function pubSetConnect(ByVal strHostIp As String,ByVal strDatabaseName As String,ByVal strUserName As String,ByVal strUserPassword As String) As String       '生成数据库连接字符串
        Return "Data Source=" & strHostIp _
                & ";Database=" & strDatabaseName _
                & ";Initial Catalog=" & strDatabaseName _
                & ";User ID=" & strUserName _
                & " ;Password =" & strUserPassword
    End Function
连接数据库主子程

Public Function pubInit() As Boolean         '连接数据库主子程
        pubConnection.Close()
        pubConnection.ConnectionString = pubSetConnect(ServerIP,DatabaseName,ServerName,ServerPassword)
        Try
            If pubConnection.State = ConnectionState.Closed Then
                pubConnection.open()
            End If
        Catch ex As sqlClient.sqlException
            MsgBox(ex.Message)
            Exit Function
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Function
        End Try
        pubsqlCommand.Connection = pubConnection
        Return True
    End Function

 '执行无返回值的sql语句
    Public Function pubMyExecuteNonQuery(ByRef myCommand As sqlCommand,ByVal strsql As String,ByRef errMsg As String) As Boolean
        myCommand.Parameters.Clear()
        myCommand.CommandType = CommandType.Text
        Try
            myCommand.CommandText = strsql
            If myCommand.Connection.State = ConnectionState.Closed Then
                myCommand.Connection.open()
            End If
            myCommand.ExecuteNonQuery()
        Catch ex As sqlException
            errMsg = "sql_err=" & CStr(ex.ErrorCode) & "|" & ex.Message
            Return False
        Catch ex As Exception
            errMsg = "other|" & ex.Message
            Return False
        End Try
        Return True
    End Function

    '根据传入的sql语句得到数据集
    Public Function pubMyExecuteQuery(ByRef myCommand As sqlCommand,ByRef errMsg As String,ByRef dsReturn As DataSet) As Boolean
        myCommand.Parameters.Clear()
        myCommand.CommandType = CommandType.Text
        dsReturn.Clear()
        Try
            myCommand.CommandText = strsql
            If myCommand.Connection.State = ConnectionState.Closed Then
                myCommand.Connection.open()
            End If
            Dim adapter As sqlDataAdapter = New sqlDataAdapter
            adapter.SelectCommand = myCommand
            adapter.Fill(dsReturn,"tmpTable")
        Catch ex As sqlException
            errMsg = "sql_err=" & CStr(ex.ErrorCode) & "|" & ex.Message
            Return False
        Catch ex As Exception
            errMsg = "other|" & ex.Message
            Return False
        End Try
        Return True
    End Function

使用案例如下:


Dim strsql As String = "SELECT * From Test"
Dim dsTable As New DataSet
If pubMyExecuteQuery(pubsqlCommand,strsql,Err,dsTable) = False Then
	MsgBox("读取数据失败!" & vbCrLf & Err,MsgBoxStyle.Exclamation,"提示")
	Exit Sub
End If
For Each pRow As DaTarow In dsTable.Tables(0).Rows
	'用pRow("id").ToString 展示出每一个数据集内容
Next

注:使用前需要调用一次pubInit这个过程。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...