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.
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.OdbcConne ction(ODBC ConnectStr ing)
Try
'To retrive values from database
Dim svlQuery = "Select * From Table1"
Dim CommandObject As New System.Data.odbc.OdbcComma nd(svlQuer y.ToString .Trim, ConnectionObject)
ConnectionObject.Open()
Dim rsRecordSet As System.Data.Odbc.OdbcDataR eader
rsRecordSet = CommandObject.ExecuteReade r
While rsRecordSet.Read()
'MsgBox(rsRecordSet.GetVal ue(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.OdbcComma nd(svlQuer y, ConnectionObject)
ConnectionObject.Open()
CommandObject1.ExecuteNonQ uery()
ConnectionObject.Close()
'To delete values to database
Dim CommandObject2 As New System.Data.Odbc.OdbcComma nd("Delete From Table1 Where ID = '1'", ConnectionObject)
ConnectionObject.Open()
CommandObject1.ExecuteNonQ uery()
ConnectionObject.Close()
Catch MyException As Exception
MsgBox(MyException.ToStrin g)
End Try
ConnectionObject = Nothing
Bye
Ajai
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.OdbcConne
Try
'To retrive values from database
Dim svlQuery = "Select * From Table1"
Dim CommandObject As New System.Data.odbc.OdbcComma
ConnectionObject.Open()
Dim rsRecordSet As System.Data.Odbc.OdbcDataR
rsRecordSet = CommandObject.ExecuteReade
While rsRecordSet.Read()
'MsgBox(rsRecordSet.GetVal
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.OdbcComma
ConnectionObject.Open()
CommandObject1.ExecuteNonQ
ConnectionObject.Close()
'To delete values to database
Dim CommandObject2 As New System.Data.Odbc.OdbcComma
ConnectionObject.Open()
CommandObject1.ExecuteNonQ
ConnectionObject.Close()
Catch MyException As Exception
MsgBox(MyException.ToStrin
End Try
ConnectionObject = Nothing
Bye
Ajai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdataodbcodbcdatareaderclasstopic.asp