我听人用OleDb做的
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb.OleDbDataAdapter
Imports System.Data.OleDb.OleDbDataReader
Public Class User
Private UserClientSocketR As Eternal.AsyncSocket.DataReceivedEventArgs
Private UserClientDataCN As OleDb.OleDbConnection
Private UserClientDataCM As OleDb.OleDbCommand
Private UserClientDataDA As OleDb.OleDbDataAdapter
Private UserClientDataDR As OleDb.OleDbDataReader
接下来我知道是要UserClientDataCN.Open()
但是我不知道Open()中的参数怎么写
接下来对数据的操作该怎么写
希望各位大哥指教!!!
Creating a Database
Before tables or other objects can be defined, the database itself must be created. The following code creates and opens a new Microsoft Jet database.
DAO
Sub DAOCreateDatabase()
Dim db As DAO.Database
Set db = DBEngine.CreateDatabase(".\New.mdb", dbLangGeneral)
As discussed earlier in this article (see the section "ADOX: Data Definition and Security"), the DAO Database object corresponds to the Catalog object in ADOX. So, to create a new Jet database using ADOX, you use the Catalog objects Create method.
In the preceding DAO code the Locale parameter is specified as dbLangGeneral. In the ADOX code, locale is not explicitly specified. The default locale for the Microsoft Jet Provider is equivalent to dbLangGeneral. Use the ADO Locale Identifier property to specify a different locale.
In DAO CreateDatabase also can take a third Options parameter, specifying information for encryption and database version. For example, the following line is used to create an encrypted, version 1.1 Microsoft Jet database:
Set db = DBEngine.CreateDatabase(".\New.mdb", dbLangGeneral, _
dbEncrypt Or dbVersion11)
In ADO encryption and database version information is specified by provider-specific properties. With the Microsoft Jet Provider, use the Encrypt Database and Engine Type properties, respectively. The following line of code specifies these values in the connection string to create an encrypted, version 1.1 Microsoft Jet database:
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\New.mdb;" & _
"Jet OLEDB:Encrypt Database=True;" & _
"Jet OLEDB:Engine Type=2;"
Public Class DataConnnection
Private m_strConnection As String
Private m_strConnection2 As String
Private myConnection As New SqlConnection()
Private myCommand As SqlCommand
Private myAdapter As SqlDataAdapter
Private MyTransaction As SqlTransaction
#Region "构造函数"
Public Sub New()
MyBase.new()
m_strConnection = "Data Source=HOME;User ID=sa;password=;initial catalog=DB_CZLK;Connect Timeout=30"
m_strConnection2 = "Data Source=HOME;User ID=sa;password=;initial catalog=NorthWind;Connect Timeout=30"
End Sub
#End Region
#Region "连接数据库"
Public Sub ConnectDatabase()
Dim StrWrong As String
Try
If myConnection.State = ConnectionState.Closed Then
myConnection.ConnectionString = m_strConnection
myConnection.Open()
End If
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region "连接数据库为了恢复数据库"
Public Sub ConnectDatabaseForRestore()
Dim StrWrong As String
Try
If myConnection.State = ConnectionState.Closed Then
myConnection.ConnectionString = m_strConnection2
myConnection.Open()
End If
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region "开始事务"
Public Sub BeginTransaction()
MyTransaction = myConnection.BeginTransaction(IsolationLevel.Serializable)
End Sub
#End Region
#Region "提交事务"
Public Sub CommitTransaction()
MyTransaction.Commit()
End Sub
#End Region
#Region "回滚事务"
Public Sub RollBackTransaction()
MyTransaction.Rollback()
End Sub
#End Region
#Region "用语句查询表(SQL语句)"
Public Sub SelectTableBySql(ByVal SQlcommand As String, ByRef MyDataset As DataSet, ByVal TableName As String)
Dim StrWrong As String
Try
myAdapter = New SqlDataAdapter(SQlcommand, myConnection)
If Not (MyDataset.Tables(TableName) Is Nothing) Then
MyDataset.Tables(TableName).Clear()
MyDataset.Tables(TableName).Dispose()
End If
myAdapter.Fill(MyDataset, TableName)
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region "用语句查询表(SQL语句)有事务的"
Public Sub SelectTableBySqlWith(ByVal SQlcommand As String, ByRef MyDataset As DataSet, ByVal TableName As String)
Dim StrWrong As String
Try
myCommand = New SqlCommand(SQlcommand, myConnection, MyTransaction)
myAdapter = New SqlDataAdapter(myCommand)
If Not (MyDataset.Tables(TableName) Is Nothing) Then
MyDataset.Tables(TableName).Clear()
MyDataset.Tables(TableName).Dispose()
End If
myAdapter.Fill(MyDataset, TableName)
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
#End Region
#Region "执行(SQL语句)"
Public Function ExcuteSQL(ByVal SQlcommand As String) As Integer
Dim StrWrong As String
Dim resultLine As Integer
Try
myCommand = New SqlCommand(SQlcommand, myConnection)
resultLine = myCommand.ExecuteNonQuery()
myCommand.Dispose()
Return resultLine
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return -1
End Try
End Function
#End Region
#Region "执行(SQL语句)有事务的"
Public Function ExcuteSQLWith(ByVal SQlcommand As String) As Integer
Dim StrWrong As String
Dim resultLine As Integer
Try
myCommand = New SqlCommand(SQlcommand, myConnection, MyTransaction)
resultLine = myCommand.ExecuteNonQuery()
myCommand.Dispose()
Return resultLine
Catch e As System.Exception
StrWrong = "有错误发生!" & vbCrLf & _
"来源:" & e.Source & vbCrLf & _
"错误:" & e.Message
MessageBox.Show(StrWrong, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
MyTransaction.Rollback()
Return -1
End Try
End Function
#End Region
#Region "关闭数据库"
Public Sub CloseConnection()
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Sub
#End Region