Home
COPC32 : Example Code: OPC to MS SQL Server DataLogger PDF Print E-mail
Written by Administrator   
Tuesday, 07 June 2016 15:14

VB.Net Example Code (Visual Studio 2015) to create OPC Data Logger (into MS SQL Server).

 

 

Example Code 1#:

 

Private Sub Insert()

 

Dim sql As String = ” INSERT INTO table3([Time_Date], [val1], [val2], [val3]) VALUES (@Time_Date, @val1, @val2, @val3)”

 

          Dim param(3) As SqlParameter

        param(0) = New SqlParameter(“@Time_Date”, SqlDbType.DateTime)
param(0).Value = DateTime.Now

        param(1) = New SqlParameter(“@val1”, SqlDbType.Float)
param(1).Value = Axcopc1.GetVl(0)

        param(2) = New SqlParameter(“@val2”, SqlDbType.Float)
param(2).Value = Axcopc1.GetVl(1)

        param(3) = New SqlParameter(“@val3”, SqlDbType.Float)
param(3).Value = Axcopc1.GetVl(2)

 

Dim sConnection As New SqlConnection(“server=(local)\sqlexpress;uid=user;pwd=12345;database=TWXLog”)
sConnection.Open()

 

Dim command As SqlCommand = sConnection.CreateCommand()
command.CommandText = sql
        command.Parameters.AddRange(param)
        Dim nResult As Integer = command.ExecuteNonQuery()

        sConnection.Close()
command.Dispose()

 

End Sub

 

 

 

Example Code 2# (For many values):

 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick


       Dim tmpStr As String

       For i = 1 To 4
           tmpStr = tmpStr + ", @val" & i
       Next

 

       Dim sql As String = ” INSERT INTO t3([Time_Date], [v1], [v2], [v3], v4) VALUES (@Time_Date" & tmpStr & ")”

       Dim param(4) As SqlParameter

       param(0) = New SqlParameter(“@Time_Date”, SqlDbType.DateTime)
       param(0).Value = DateTime.Now

 

       For i = 1 To 4
           param(i) = New SqlParameter(“@val” & i, SqlDbType.Float)
           param(i).Value = Axcopc1.GetVl(i - 1)
       Next


       Dim sConnection As New SqlConnection(“server=(local);uid=usr1;pwd=123456;database=test”)
       sConnection.Open()


       Dim command As SqlCommand = sConnection.CreateCommand()
       command.CommandText = sql
       command.Parameters.AddRange(param)
       Dim nResult As Integer = command.ExecuteNonQuery()

       sConnection.Close()
       command.Dispose()
   End Sub

Note:

Need SqlClient library by adding following code on the top of code

 

Imports System.Data.SqlClient

Last Updated ( Tuesday, 07 June 2016 08:01 )
 

สิ่งที่น่าสนใจ

L53U Multi Power Transducer
L53U Multi Power Transducer
Single-phase / 2-wire and 3-wire, three-pase / 3-wire and 4-wire systems
More InformationPdf DownloadSpecification