This example shows how to record OPC Data To My SQL.
Download Example Project(4/2016)
(for Visual Studio 2015/Express)
Download Example Project(OLD)(for Visual Studio 2015/Express)
VDO version
Requirement
– COPC32
– Visual Studio or Visual Studio Express
– MySQL Server and MySQL Data.dll
Create New Windows Project with Visual Studio and add reference of MySQL Data.dll in to you project.
Insert Controls, COPC32, Labels and Timer shown below.
Configure COPC32 properties connect to OPC Server, OPC tags.
For Timer, set interval property to trigger recording MySQL, for instance 5 seconds.
All example code shown below.
Imports MySql.Data.MySqlClient Public Class Form1 Dim v(0 To 2) As Double Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Axcopc1.cnnec() End Sub Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing Axcopc1.discnn() End Sub Private Sub Axcopc1_datChngX(sender As Object, e As AxCOPC32.__copc_datChngXEvent) Handles Axcopc1.datChngX Dim i As Integer For i = 0 To 2 v(i) = Axcopc1.GetVl(i) Next Label1.Text = v(0) Label2.Text = v(1) Label3.Text = v(2) End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim objConn As New MySqlConnection Dim objCmd As New MySqlCommand Dim strConnString, strSQL As String Dim intNumRows As Integer strConnString = "Server=localhost;User Id=root; Password=123456; Database=mydb; Pooling=false" objConn.ConnectionString = strConnString objConn.Open() strSQL = "INSERT INTO t1 values (NOW()," & v(0) & "," & v(1) & "," & v(2) & ")" objCmd = New MySqlCommand(strSQL, objConn) intNumRows = objCmd.ExecuteScalar() objConn.Close() objConn = Nothing End Sub End Class
The example record OPC data to MySQL table, “t1” in database “mydb”. Table “t1” has 4 column, Time_Date, v1, v2 and v3.
Result of the example.