ADO Recordset什么都不返回

时间:2017-06-03 02:17:32

标签: mysql vb.net ado

我正在使用ADO Recorset访问MySQL表,但ADO Recordset不返回记录。我在Visual Studio中使用VB.NET。我可以建立与数据库的通信,建立连接,创建表,所有通过连接对象(如下所示),但在通过Recordset返回信息时,给出错误。

Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim fld As ADODB.Field
Dim aux As String

aux = "Provider = MSDASQL;Driver={MySQL ODBC 5.2 ANSI Driver}; Server=localhost; Database = db-teste; User=root; Password=123456; Option=3;"
conn.Open(aux)

'create table
conn.Execute("DROP TABLE IF EXISTS my_ado")
conn.Execute("CREATE TABLE my_ado(id int Not null primary key, name varchar(20)," &
                "txt text, dt date, tm time, ts timestamp)")
'insert
conn.Execute("INSERT INTO my_ado(id,name,txt) values(1,100,'venu')")
conn.Execute("INSERT INTO my_ado(id,name,txt) values(2,200,'MySQL')")

'============================================================================
' UNTIL HERE, IT WORKS FINE!
'============================================================================


' This does not get anything
rs.Open("SELECT * FROM my_ado", conn)
rs.MoveFirst()

' RecordCount always returns "-1" <<<<<<====================
Debug.Print(Str(rs.RecordCount()))

' rs.Fields = ERROR
For Each fld In rs.Fields 
    Debug.Print(fld.Name)
Next

有什么问题?

1 个答案:

答案 0 :(得分:0)

检查数据库配置以确保AUTO COMMIT开关已打开,否则您需要提交数据。我并不完全熟悉MySQL,但我知道INSERT需要以某种方式提交。

这篇文章解释了一切:

https://dba.stackexchange.com/questions/4252/do-inserts-get-auto-committed

rs.Open("SELECT * FROM my_ado", conn)代码行之后将其添加到您的代码中:

' Create dataset and data adapter objects
Dim ds As New DataSet("Recordset")
Dim da As New OleDbDataAdapter()

' Call data adapter's Fill method to fill data from ADO
' Recordset to the dataset

da.Fill(ds, rs, "Data")

数据应位于ds.Tables!Data,您可以查看此行:

ds.Tables!Data.Rows.Count