Copy data from Datagridview To MS SQL Database

Posted on 2011-05-11
Last Modified: 2013-12-16
My Datagridview has 4 columns:

ID    Name     Quantity   SafetyQuantity

My MS SQL has 2 columns:

Name         Quantity

and I want to copy from my Datagridview to MS SQL:
Name --> Name
Quantity --> Quantity

How can I do that with VS C#?

Question by:JameMeck
    LVL 14

    Expert Comment

    on that link, used CommandBuilder
    xxxCommandBuilder cmdbuilder = new xxxCommandBuilder(xdataadapter);
    xdataadapter.Update(xdatatable, "xyourtable");
    LVL 41

    Expert Comment

    Let's see if we can clarify things a wee  bit...

    Did the contents of the grid originally come from the SQL Server?  If so, then you'd use the same kind of database features (TableAdapter, DataTables, etc) to send the updated data back to the SQL server.  Typically this would be via the TableAdapter's Update method.

    On, the other hand, if the data came from some other source (like an excel spreadsheet, by hand, etc), then you'd need to do a bit more work.
    LVL 12

    Accepted Solution

    Use "for" statement to loop through each row and execute SQL statement to insert each row in the database.

    You can also save the data from DataSet to your database:

    Author Closing Comment

    You are right, a little slow, but completed my job.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now