Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7924109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:33:54+00:00 2026-06-03T17:33:54+00:00

I have a datagridview which is created by various action and user’s manipulation of

  • 0

I have a datagridview which is created by various action and user’s manipulation of data.
I want to insert all the data of the gridview to the database at once, I know I could try a code similar to this:

for(int i=0; i< dataGridView1.Rows.Count;i++)
    {
        string StrQuery= @"INSERT INTO tableName VALUES (" + dataGridView1.Rows[i].Cells["ColumnName"].Value +", " + dataGridView1.Rows[i].Cells["ColumnName"].Value +");";

      try
      {
        using (SqlConnection conn = new SqlConnection(ConnString))
        {
            using (SqlCommand comm = new SqlCommand(StrQuery, conn))
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
        }
      }

But will it be fair to create a new connection every time a record is inserted? The datagrid may contain many rows… Is there any way to take off all the data to the server at once and loop inside in sql to insert all the data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:34:01+00:00Added an answer on June 3, 2026 at 5:34 pm

    If you move your for loop, you won’t have to make multiple connections. Just a quick edit to your code block (by no means completely correct):

    string StrQuery;
    try
    {
        using (SqlConnection conn = new SqlConnection(ConnString))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = conn;
                conn.Open();
                for(int i=0; i< dataGridView1.Rows.Count;i++)
                {
                    StrQuery= @"INSERT INTO tableName VALUES (" 
                        + dataGridView1.Rows[i].Cells["ColumnName"].Text+", " 
                        + dataGridView1.Rows[i].Cells["ColumnName"].Text+");";
                    comm.CommandText = StrQuery;
                    comm.ExecuteNonQuery();
                }
            }
        }
    }
    

    As to executing multiple SQL commands at once, please look at this link:
    Multiple statements in single SqlCommand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView which is filling from Table in SQL Server Database. One
I have a datagridview and a combobox which get populated randomly with data. However,
I have next code which creates a print page with data from datagridview. Font
I have next code which creates a print page with data from datagridview. Font
I Have a Datagridview bound to a datatable wich hold data from a database
Im using c# .net windows form application. I have created a database which has
I have a form created which an unbound datagridview that has three columns. Position,
I have a Datagridview, on which I need to validate the user input on
I have a DataGridView which is data-bound to a BindingList. My DataGridView also has
I have a datagridview which is used for listing all the students information .I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.