简体   繁体   中英

Insert from datagridview to SQL with dynamic column names

Having imported data from an excel spreadsheet into datagridview, I would like to create a table (determined by the user in Windows forms) and insert it with the column names into the new table from the datagridview.

The user can use the "program" to insert other excel sheets into other databases as well.

How do I add column names dynamically that is not predefined and is defined by how many columns is in the excel spreadsheet?

My goal here was to create an import application to import excel spreadsheets into a SQL server database. The problem that I faced is to get the number columns that I needed to insert into the database and to dynamically name them according to the excel spreadsheet.

The application will be used on various spreadsheets and the column names must not be hardcoded. This is the code I used that managed the last phase of the application where all the data from the spreadsheet is inserted into to the database in SQL.

    //Import Button
    private void button5_Click(object sender, EventArgs e)
    {
        string createColumns = "";
        string columns = "";
        string rows = "";
        var grid = (DataTable)dataGridView3.DataSource;
        for (int i = 0; i < grid.Columns.Count; i++)
        {
            if (i == grid.Columns.Count - 1)
            {
                createColumns += "[" + grid.Columns[i].ToString() + "] varchar(200) NULL";
                columns += "[" + grid.Columns[i].ToString() + "]";
            }
            else
            {
                columns += "[" + grid.Columns[i].ToString() + "],";
                createColumns += "[" + grid.Columns[i].ToString() + "] varchar(200) NULL,";
            }

        }
        string createTable = string.Format("Create table [{0}] ({1})", textBox1.Text, createColumns);
        rows = string.Format("Insert Into[{0}]({1})", textBox1.Text, columns);
        for (int i = 0; i < grid.Rows.Count; i++)
        {
            string row = "";

            for (int c = 0; c < grid.Columns.Count; c++)
            {

                if (c == grid.Columns.Count - 1)
                    row += "'" + grid.Rows[i][c].ToString() + "'";
                else
                    row += "'" + grid.Rows[i][c].ToString() + "', ";
            }

            if (i == grid.Rows.Count - 1)
                rows += string.Format(" ({0});", row);
            else
            {
                if (i == 0)
                {
                    rows += " Values";
                }
                rows += string.Format(" ({0}),", row);
            }
        }
        string s = "Integrated Security = SSPI;Persist Security Info = False;Data Source = " +
            ServerName.Text + "; Initial Catalog = " +
            Databases.Text;

        SqlConnection conn = new SqlConnection(s);

        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = createTable;
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();

        SqlCommand cmd2 = new SqlCommand(rows, conn);
        cmd2.CommandType = CommandType.Text;
        conn.Open();
        cmd2.ExecuteNonQuery();
        conn.Close();

        Application.Exit();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM