请教初级的数据库连接问题!

bastenzl 2003-04-23 03:53:27
我想在一个Class里实现对Access数据库的读取,添加,删除等操作,哪位大哥能不能告诉我从引入开始的每一步的基本操作,最好能给我一个范例,十分感谢!
...全文
60 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bastenzl 2003-04-23
  • 打赏
  • 举报
回复
我听人用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()中的参数怎么写
接下来对数据的操作该怎么写
希望各位大哥指教!!!
hsentao 2003-04-23
  • 打赏
  • 举报
回复
楼上的你在胡言乱语什么呀,人家说的是Access数据库。
这位老兄啊,建议你看一看MSDN中关于DAO或者ADOX方面的材料,相关的内容都有,我这两天
差点死在这些问题上,现在终于过来了。有什么东西再问吧。

下边是CreatDatabase的例子:
(别的你自己找吧,上面都有例子)

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)

End Sub

ADOX
Sub ADOCreateDatabase()

Dim cat As New ADOX.Catalog

cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=.\New.mdb;"

End Sub

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;"
lionqun 2003-04-23
  • 打赏
  • 举报
回复
这是我为连接SQL Server而专门作的一个类,自己参考吧

Imports System.Data.SqlClient
Imports System.Data

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

End Class
zwztu 2003-04-23
  • 打赏
  • 举报
回复
建议先学习微软的quickstart,那是入门的好教材。

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧