1

Good Day,

I am new here and not a well experienced programmer but I somehow get by. Anyway here's my Question:

Is it possible to use the Data displayed in the DataGridView (Column names, rows, etc.) to create a table in SQL?

I was creating a simple program where the Client could import Excel files into the program, and that would be the DataGridView. And after that is the problem, I don't know how to upload the table into the SQL Server.

  • Try the other way around, it is the correct one. Import the excel in the database after that bind the datagrid from the database. In this case if you have manual changes in the data, the correct one will be shown. Search in google Import Excel in Database, DataBind GridView with DataSet. – mybirthname Sep 22 '15 at 08:05
  • Yes! But there are many solutions, can't tell you which is better, you need to find one suitable your self. – Nam Bình Sep 22 '15 at 08:14
  • @mybirthname Yes that works, I've tried it and thanks! But the problem is that the Client will personally be the one uploading the excel file into the Database. – TJ Arias Regius Sep 22 '15 at 08:20
  • There is no problem with client to import the excel. You need to create a control on which he uploads the excel and after that another button which said import and this will be from client side. – mybirthname Sep 22 '15 at 08:23
  • @mybirthname okay, I have made a button that uploads an excel file unto the DataGridView, this is working I no problem there, but how can I upload it to the SQL Server? – TJ Arias Regius Sep 22 '15 at 08:30

2 Answers2

0

Is it possible to use the Data displayed in the DataGridView (Column names, rows, etc.) to create a table in SQL?

Yes it is possible here is the example or sample code

using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("INSERT INTO MyTable(Column1, Column2) VALUES (@C1, @C2)", con))
    {
        cmd.Parameters.Add(new SqlParameter("@C1", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@C2", SqlDbType.VarChar));
        con.Open();
        foreach (DataGridViewRow row in myDataGridView.Rows)
        {
            if (!row.IsNewRow)
            {
                cmd.Parameters["@C1"].Value = row.Cells[0].Value;
                cmd.Parameters["@C2"].Value = row.Cells[1].Value;
                cmd.ExecuteNonQuery();
            }
        }
    }
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Thanks but will this still work if I upload multiple excel files, with different columns but not simultaneously? Seems like I would still need to indicate the number of columns. – TJ Arias Regius Sep 22 '15 at 08:37
  • This will work when the number of columns and datatypes are gonna match. The Table structure has to be there before we run the code. – Mohit S Sep 22 '15 at 08:40
  • Another idea might be You can read the header rows to get the name of the Columns and Create a Table before running the code. – Mohit S Sep 22 '15 at 08:41
  • ohhh okay, I'll to make a research about that, thank for the Idea! – TJ Arias Regius Sep 22 '15 at 08:53
  • Here on SO ... you can upvote(I know you haven't got reputation) or accept it as answer to say thanks :) – Mohit S Sep 22 '15 at 08:54