LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Inser data to DB by SqlBulkCopy (.NET)

Solved!
Go to solution

Hello,
Iam trying to fill DB with data from osci, but I did not used .NET yet. (BulkCopy should be the best to upload tousands of rows)
this is how I expect the VI should look like.

Pepee4_1-1710507112952.png

 

The problem is that I dont know how to create these rows/table and execute command.
Have anyone experience with this ? 

Iam using LabVIEW version 2023 Q3 64-bit

0 Kudos
Message 1 of 4
(385 Views)

I'm not familiar with this class, but it might not be as efficient as you might hope, since LV is not a .NET language and building the relevant data types also has overhead. Looking at the documentation, it looks like you can pass a DataTable or an array of DataRows (the case in your example), so you need to build one of those with the relevant data.

 

I'm not sure if it's possible to create a DataRow on its own, but here's one example of how you can create a DataTable and add rows to it:

 

 

I would suggest testing first with standard SQL commands using something like the DB connectivity toolkit or calling the ADO.NET classes in LV directly and only go for this option if the performance doesn't match what you need.


___________________
Try to take over the world!
0 Kudos
Message 2 of 4
(290 Views)

This was mine best 

Pepee4_0-1710836435678.png

Had there two problems:
when I tried to add column 4 then I get error: column 4 was not found
I did not found out how to insert GUID (uniqueidentifier) (into different table)

0 Kudos
Message 3 of 4
(279 Views)
Solution
Accepted by topic author Pepee4

Final solution was to use DLL

Pepee4_0-1710837275683.png

 



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;

namespace BulkCopyLabview
{
    public class Bulk
    {
        private string connectionString = null;

        private List<AutomaticFunctionTest_Osci_out> data;
        public Bulk(string _connectionString)
        {
            this.connectionString = _connectionString;
            this.data = new List<AutomaticFunctionTest_Osci_out>();
        }

        public void Insert(InputData obj)
        {
            Guid g = new Guid(obj.Test_ID);
            Guid newone = new Guid();
            this.data.Add(new AutomaticFunctionTest_Osci_out
            {
                Id = newone,
                CH1 = obj.CH1,
                CH2 = obj.CH2,
                CH3 = obj.CH3,
                CH4 = obj.CH4,
                Function_ID = obj.Function_ID,
                Run = obj.Run,
                Test_ID = g
            });
        }
        public string Submit()
        {
            try
            {
                if (this.connectionString == null)
                {
                    return "FAIL: ConnectionString is null";
                }
                using (var bulk = new SqlBulkCopy(this.connectionString))
                {
                    bulk.DestinationTableName = "AutomaticFunctionTest_Osci_out";
                    bulk.BulkCopyTimeout = 60;
                    bulk.WriteToServer(Helper.AsDataTable(this.data));
                }
                return "SUCCESS";
            }
            catch (Exception ex)
            {
                return "FAIL: " + ex.Message;
            }
        }
    }

    public static class Helper
    {
        public static DataTable AsDataTable<T>(this IEnumerable<T> data)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            var table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
    }

    public class InputData
    {
        public string Test_ID { get; set; }
        public int Function_ID { get; set; }
        public int Run { get; set; }
        public double CH1 { get; set; }
        public double CH2 { get; set; }
        public double CH3 { get; set; }
        public double CH4 { get; set; }
    }

    public class AutomaticFunctionTest_Osci_out
    {
        public Guid Id { get; set; }
        public Guid Test_ID { get; set; }
        public int Function_ID { get; set; }
        public int Run { get; set; }
        public double CH1 { get; set; }
        public double CH2 { get; set; }
        public double CH3 { get; set; }
        public double CH4 { get; set; }
    }
}

 

 

Download All
0 Kudos
Message 4 of 4
(276 Views)