开发者

problem connecting with odbc mysql in vb.net

I'm trying another method to connect mysql and vb.net. I didn't have any difficulties connecting mysql and vb.net when using mysql net connector. And I also used the same codes. I just replaced the ones that needed to be replaced with odbc.

 Imports System.Data.Odbc



Public Class globalclass
    Private cn As New OdbcConnection("DSN=korosu")

    Dim cmd As Odbc.OdbcCommand

    Public name As String
    Public age As String


    Public Sub New()
        cn.Open()
        cmd = New Odbc.OdbcCommand("SELECT * FROM test")

    End Sub

    Public Sub adds()

        cmd.CommandText = "INSERT INTO test(name, age) VALUES('" + name + "','" + age +开发者_高级运维 "')"
        cmd.ExecuteNonQuery()



    End Sub

What do I need to do to fix this? I always get the runtime error and its highlighting the cmd.ExecuteNonQuery. And says that the connection has not been properly initialized.Please help


You haven't specified that cmd uses cn.


you forgot to set the connection for the OdbcCommand:

cn.Open()
cmd.Connection = cn
cmd.CommandText = "INSERT INTO test(name, age) VALUES('" + name + "','" + age + "')"
cmd.ExecuteNonQuery()
cn.Close()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜