0

I have a class Connect to connect to the Database. Here is the code:

    class Connect
{
    SqlConnection con;
    public Connect()
    {
        String connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + @"\Database1.mdf;Integrated Security=True;User Instance=True";
        con = new SqlConnection(connectionString);
    }

    public DataTable executeSelect(String query)
    {
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        con.Close();

        return dt;
    }

    public void execute(String query)
    {

        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();

        con.Close();
    }
}

And i have DataGridView like this:

My data

How is the code to insert all data from that DataGridView to the Database?

  • Possible duplicate of [Insert entire DataTable into database at once instead of row by row?](http://stackoverflow.com/questions/10405373/insert-entire-datatable-into-database-at-once-instead-of-row-by-row) – Marco Forberg Dec 07 '15 at 09:28

2 Answers2

0

If you write your last sentence to Google you will get this link at first. And your answer is there.

Community
  • 1
  • 1
Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36
0

I think you need to use loop insertion.You have to put all data in a list. Or try this

because you have a DataTable in your post codes.

Community
  • 1
  • 1
MapleStory
  • 628
  • 3
  • 11
  • 22