1
Open Discussion / Re: write to a database from datagridview
« on: June 26, 2022, 05:40:34 AM »
There is any other solution to convert data from Firebird to Mysql database.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Imports System.Configuration
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.Data.DataTable
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Data
Imports System.Data.Odbc.OdbcCommand
Imports System.Data.OleDb
Public Class MainForm
Public conn_odbc As OdbcConnection
Public conn1 As OdbcConnection
Public conn2 As OdbcConnection
Public ConnMysql_protocol As MySqlConnection
Public Sub connection_to_firebird_prod()
Dim str_odbc_prod As String
str_odbc_prod = "DRIVER=Firebird/InterBase(r) driver;UID=****;PWD=****;DBNAME=*****.GDB;"
conn1 = New OdbcConnection(str_odbc_prod)
End Sub
Public Sub production()
Dim str_odbc_prod As String
str_odbc_prod = "DRIVER=Firebird/InterBase(r) driver;UID=****;PWD=****;DBNAME=****.GDB;"
Dim odbc_connection_prod As New OdbcConnection(str_odbc_prod)
Dim odbc_adapter_prod As New OdbcDataAdapter("select ID,NR,HR from FrirebirdTable", odbc_connection_prod)
Dim table_odbc_prod As New DataTable
odbc_adapter_prod.Fill(table_odbc_prod)
DataGridView_production.DataSource = table_odbc_prod
odbc_adapter_prod.Dispose()
Dim mysql_connection_prod As New MySqlConnection("server=localhost;user id=****;database=mysqldatabase")
Dim insert_in_mysql_prod As New MySqlCommand("INSERT INTO `mysqltable` (`ID`, `NR`, `HR`) VALUES (@ID,@NR,@HR)", mysql_connection_prod)
Try
mysql_connection_prod.Open()
Catch exError As MySqlException
End Try
For i As Integer = 0 To DataGridView_production.RowCount - 1 Step +1
insert_in_mysql_prod.Parameters.Add("@ID", MySqlDbType.Int64).Value = DataGridView_production.Rows(i).Cells(0).Value.ToString
insert_in_mysql_prod.Parameters.Add("@NR", MySqlDbType.Int64).Value = DataGridView_production.Rows(i).Cells(1).Value.ToString
insert_in_mysql_prod.Parameters.Add("@HR", MySqlDbType.Int64).Value = DataGridView_production.Rows(i).Cells(2).Value.ToString
insert_in_mysql_prod.ExecuteNonQuery()
Next
mysql_connection_prod.Close()
End Sub
End Class
Hello, I leave the link with the siemens driver using snap7, in front of any doubt I will try to help as much as I can since I am not an expert in .net programming, I do what I can. I have tested it with snap7's own simulator and with an S7-1500 PLC. (sorry my english is translated)
https://drive.google.com/open?id=1qkjWns0W4z6uQtiWrUeaWBJl4oHMxAaC
Dim conString As String = "server=localhost;userid=root;password=;database=my_database"
Using con As New MySqlConnection(conString)
Using cmd As New MySqlCommand
With cmd
.Connection = con
.CommandText = "insert into my_database.my_table (var1) values (@var1)"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@var1", var1)
End With
Try
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message + System.Environment.NewLine, "MySQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
End Using