开发者

Insert new records into a table with Visual Basic using OLEDBConnection

I'm using Visual Basic 2010 Express and Access 2003. I'm trying to make sql querys to the mdb file. I'm using OLEDBConnection. The Select query works fine, but I can't insert rows in the table. Here is the code.

    Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0;data source=" & System.IO.Directory.GetCurrentDirectory() & "\tpv.mdb;" 
    Dim con As New OleDb.OleDbConnection(connStr)
    con.Open()
    Dim query As String = "select * from Pro开发者_如何学Goductos"
    Dim cmd As New OleDb.OleDbCommand(query, con)
    Dim reader As OleDb.OleDbDataReader
    reader = cmd.ExecuteReader
    While reader.Read()
        MsgBox(reader.GetValue(0) & ", " & reader.GetValue(1) & " , " & reader.GetValue(2))
    End While
    reader.Close()
    query = "insert into Productos (NombreProducto,PrecioCoste) VALUES ('cana',4)"
    Dim cmd2 As New OleDb.OleDbCommand(query, con)
    cmd.ExecuteNonQuery()
    con.Close()

Why the INSERT query does not work?


Ok, I found my stupid problem. Althoug I have declared 2 OleDbCommands, I referenced the first one in both cases

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜