'Create generic SQL INSERT statement from datatable with looping

I'm trying to create a generic method of taking a datatable and creating a SQL INSERT statement from its contents.

I've spent the better part of a couple of days researching this on StackOverflow.

I just need some help with the looping section code--it doesn't give the desired results and I've reached the limit of my coding skills.

I don't need BulkCopy solutions or database specific ones. I just need the INSERT string. I don't care about SQL injections or anything else. This is an internal legacy app that needs to have a few repetitive/manual processes automated.

I have written an example console program to demonstrate. The size of the datatable isn't really a factor form me. It is never over 60 rows and has 6 columns:

The datatable should look like:

ID  Group   X       Y     Z
1   A       100     200   400
2   B       200     400   800
3   C       300     600   1200
4   D       400     800   1600
5   E       500     1000  2000

The example console app is:

using System;
using System.Data;

namespace crSQLInsert
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.Add("ID");
            dt.Columns.Add("Group");
            dt.Columns.Add("X");
            dt.Columns.Add("Y");
            dt.Columns.Add("Z");

            for (int i = 0; i < 5; i++)
            {
                Random rnd = new Random();
                char cLtr = Convert.ToChar(i+65);
                double colVal = ((i + 1) * 100) ;
                DataRow dr = dt.NewRow();
                dr["ID"] = 1 + i;
                dr["Group"] = cLtr;
                dr["X"] = colVal ;
                dr["Y"] = colVal * 2;
                dr["Z"] = colVal * 4;
                dt.Rows.Add(dr);
            }

            createSQL(dt);
        }


        public static void createSQL(DataTable dtSQL)
        {
            // Create generic SQL query to add datatable contents to my_table
            string sSQL = "";
            sSQL += "INSERT INTO my_table (ID, Group, X, Y, Z) VALUES (";

            for (int i = 0; i < dtSQL.Rows.Count; i++)
            {

                for (int j = 0; j < dtSQL.Columns.Count; j++)
                {
                    sSQL += dtSQL.Rows[i][j].ToString().Trim();
                    if (j != dtSQL.Columns.Count - 1)
                    {
                        sSQL += "','";
                    }
                    else
                    {
                        sSQL += "')";
                    }
                }
            }

            Console.WriteLine(sSQL);
        }
    }
}

The resulting SQL string should be like below:

(I've formatted it so that it's easier to read--the actual string doesn't necessarily need the \r\n as it will end up as a string in an ODBCCommand().

INSERT INTO my_table (ID, Group, X, Y, Z) 
 VALUES (1','A','100','200','400'),
        (2','B','200','400','800'),
        (3','C','300','600','1200'),
        (4','D','400','800','1600'),
        (5','E','500','1000','2000');

Thanks for any help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source