Link to home
Start Free TrialLog in
Avatar of webusername
webusername

asked on

inserting into mysql using vb.net

hi, i designed a vb.net application that has fields . how do i do to store the user inputs (textfield,combo box) from the interface to the database that is mysql?I use odbc for my connection. I have an idea that i am supposed to use datareader or some like that.
Avatar of arif_eqbal
arif_eqbal

Well You'll need ODBCDataReader and ODBCCommand objects for reading and writing data to MySql look for the following link


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataodbcodbcdatareaderclasstopic.asp

Hai Try This Sample

        Dim Server As String = "ngws01", LaBase As String = "CMPAjaiDev", User As String = "CMPAjaiDev", Password As String = "CMPAjaiDev"
        Dim ODBCConnectString As String = _
        "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & Server _
        & ";DATABASE=" & LaBase & ";UID=" & User _
        & ";PASSWORD=" & Password & ";OPTION=3;"


        Dim ConnectionObject As New System.Data.Odbc.OdbcConnection(ODBCConnectString)
        Try
            'To retrive values from database
            Dim svlQuery = "Select * From Table1"
            Dim CommandObject As New System.Data.odbc.OdbcCommand(svlQuery.ToString.Trim, ConnectionObject)
            ConnectionObject.Open()
            Dim rsRecordSet As System.Data.Odbc.OdbcDataReader
            rsRecordSet = CommandObject.ExecuteReader
            While rsRecordSet.Read()
                'MsgBox(rsRecordSet.GetValue(1))
            End While
            rsRecordSet.Close()
            rsRecordSet = Nothing
            ConnectionObject.Close()

            'To add values to database
            Dim ComboBoxValue As String = "1"
            Dim TextBoxValue As String = "One"
            svlQuery = "Insert Into Table1 Values ('" + ComboBoxValue + "', '" + TextBoxValue + "')"
            Dim CommandObject1 As New System.Data.Odbc.OdbcCommand(svlQuery, ConnectionObject)
            ConnectionObject.Open()
            CommandObject1.ExecuteNonQuery()
            ConnectionObject.Close()

            'To delete values to database
            Dim CommandObject2 As New System.Data.Odbc.OdbcCommand("Delete From Table1 Where ID = '1'", ConnectionObject)
            ConnectionObject.Open()
            CommandObject1.ExecuteNonQuery()
            ConnectionObject.Close()
        Catch MyException As Exception
            MsgBox(MyException.ToString)
        End Try


        ConnectionObject = Nothing

Bye
Ajai
ASKER CERTIFIED SOLUTION
Avatar of ajaikumarr
ajaikumarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hai,

Note You have to change the follow things

Variable declartions [Server, DataBaseName, User, Password] to your server details

[Dim svlQuery As String = "Insert Into TableName Values ('" + Me.ComboBox1.Text + "', '" + Me.TextBox1.Text + "')"] to your comboname & textbox name and also change the tablename.

Bye
Ajai