mysql movenext_MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法示例 (VB)

MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法示例 (VB)MoveFirst, MoveLast, MoveNext, and MovePrevious methods example (VB)

09/18/2015

本文内容

适用于:Access 2013、Office 2013Applies to: Access 2013, Office 2013

本示例使用 MoveFirst、MoveLast、MoveNext 和 MovePrevious 方法基于提供的命令移动 Recordset 的记录指针。若要使该过程运行,需要 MoveAny 过程。This example uses the MoveFirst, MoveLast, MoveNext, and MovePrevious methods to move the record pointer of a Recordset based on the supplied command. The MoveAny procedure is required for this procedure to run.

'BeginMoveFirstVB

'To integrate this code

'replace the data source and initial catalog values

'in the connection string

Public Sub Main()

On Error GoTo ErrorHandler

' connection and recordset variables

Dim rstAuthors As ADODB.Recordset

Dim Cnxn As ADODB.Connection

Dim strCnxn As String

Dim strSQLAuthors

' record variables

Dim strMessage As String

Dim intCommand As Integer

' Open connection

Set Cnxn = New ADODB.Connection

strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _

"Initial Catalog='Pubs';Integrated Security='SSPI';"

Cnxn.Open strCnxn

' Open recordset from Authors table

Set rstAuthors = New ADODB.Recordset

rstAuthors.CursorLocation = adUseClient

' Use client cursor to enable AbsolutePosition property

strSQLAuthors = "Authors"

rstAuthors.Open strSQLAuthors, Cnxn, adOpenStatic, adLockReadOnly, adCmdTable

' Show current record information and get user's method choice

Do

strMessage = "Name: " & rstAuthors!au_fname & " " & _

rstAuthors!au_lname & vbCr & "Record " & _

rstAuthors.AbsolutePosition & " of " & _

rstAuthors.RecordCount & vbCr & vbCr & _

"[1 - MoveFirst, 2 - MoveLast, " & vbCr & _

"3 - MoveNext, 4 - MovePrevious]"

intCommand = Val(Left(InputBox(strMessage), 1))

' for exiting the loop

If intCommand < 1 Or intCommand > 4 Then

MsgBox "You either entered a non-number or canceled the input box. Exit the application."

Exit Do

End If

' Use specified method while trapping for BOF and EOF

Select Case intCommand

Case 1

rstAuthors.MoveFirst

Case 2

rstAuthors.MoveLast

Case 3

rstAuthors.MoveNext

If rstAuthors.EOF Then

MsgBox "Already at end of recordset!"

rstAuthors.MoveLast

End If

Case 4

rstAuthors.MovePrevious

If rstAuthors.BOF Then

MsgBox "Already at beginning of recordset!"

rstAuthors.MoveFirst

End If

End Select

Loop

' clean up

rstAuthors.Close

Cnxn.Close

Set rstAuthors = Nothing

Set Cnxn = Nothing

Exit Sub

ErrorHandler:

' clean up

If Not rstAuthors Is Nothing Then

If rstAuthors.State = adStateOpen Then rstAuthors.Close

End If

Set rstAuthors = Nothing

If Not Cnxn Is Nothing Then

If Cnxn.State = adStateOpen Then Cnxn.Close

End If

Set Cnxn = Nothing

If Err <> 0 Then

MsgBox Err.Source & "-->" & Err.Description, , "Error"

End If

End Sub

'EndMoveFirstVB

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB可以使用MySQL提供的ODBC驱动程序连接到MySQL服务器并操作MySQL数据库。以下是一个简单的示例代码: 连接到MySQL数据库: ```vb Dim conn As New ADODB.Connection Dim connString As String = "Driver={MySQL ODBC 8.0 ANSI Driver};Server=localhost;Database=mydb;User=myuser;Password=mypass;Option=3;" conn.ConnectionString = connString conn.Open() ``` 查询MySQL数据库: ```vb Dim rs As New ADODB.Recordset rs.Open("SELECT * FROM mytable", conn) While Not rs.EOF Console.WriteLine(rs.Fields("field1").Value) rs.MoveNext() End While rs.Close() ``` 插入数据到MySQL数据库: ```vb Dim cmd As New ADODB.Command cmd.ActiveConnection = conn cmd.CommandText = "INSERT INTO mytable (field1, field2) VALUES (?, ?)" cmd.Parameters.Append(cmd.CreateParameter("p1", adVarChar, adParamInput, 50, "value1")) cmd.Parameters.Append(cmd.CreateParameter("p2", adVarChar, adParamInput, 50, "value2")) cmd.Execute() ``` 在上面的示例代码中,我们首先创建了一个ADODB.Connection对象来连接到MySQL数据库。连接字符串指定了ODBC驱动程序的名称、MySQL服务器地址、数据库名称、用户名和密码等信息。 然后我们使用ADODB.Recordset对象查询MySQL数据库中的数据,并使用Fields属性获取每一行的字段值。 最后,我们创建了一个ADODB.Command对象,并使用CreateParameter方法添加了两个参数,然后执行了一个插入数据的SQL命令。 希望这个示例可以帮助你了解如何使用VB连接和操作MySQL数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值