前端之家收集整理的这篇文章主要介绍了
VB.NET怎么连接数据库,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
'sql版
Private Con As String = "<连接字符串>"'<连接字符串>可以自动生成
Private connection As sqlClient.sqlConnection
Private command As sqlClient.sqlCommand
Public Function DBopen() As Boolean '打开数据库
Try
connection = New sqlClient.sqlConnection
connection.ConnectionString = Con
connection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Public Function DBclose() As Boolean '关闭数据库
Try
connection = New sqlClient.sqlConnection
connection.ConnectionString = Con
connection.Close()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
'Access版
Private Con As String = "<连接字符串>"'<连接字符串>可以自动生成
Private connconnection As OleDb.OleDbConnection
Private conncommand As OleDb.OleDbCommand
Public Function DBopen() As Boolean
Try
Me.connconnection = New OleDb.OleDbConnection
Me.connconnection.ConnectionString = Con
connconnection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Public Function DBclose() As Boolean
Try
Me.connconnection = New OleDb.OleDbConnection
Me.connconnection.ConnectionString = Con
connconnection.Close()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
原文链接:https://www.f2er.com/vb/259069.html