Write Unbound DGV to DB

Posted on 2013-01-02
Last Modified: 2013-01-02
I have code calculating and displaying results to a DataGridView which I'd like to save to database. I'm finding solutions to update results that were populated via a DataAdapter, but this data wasn't queried.

I may have as many as 1000 rows, so I'd rather not Insert line by line (VPN connection can be terrible).  The only bulk method I know for SQL Server 2005 is to  
INSERT INTO MyTable (Field1, Field2)
SELECT Row1Val1, Row1Val2

I don't need to check for duplicates or any validation, just shove it all into a table.
Question by:MrBullwinkle
  • 3
  • 2
LVL 13

Expert Comment

ID: 38737009
Since you're in SQL 2k5 you can't use table parameters (yet), so I think you're right.
Probably your best bet would be to dynamically create a string with your insert query.

First create the INSERT part, something like:

string strQuery = "INSERT INTO myTable(Field1, ... , FieldN)

Then loop through all the rows of your dgv and append to that string each and every one of your inserts.

Hope it helps.
LVL 13

Author Comment

ID: 38737052
I was hoping there was some way to utilize the DataAdapter.

From the lack of response, seems that StringBuilder probably is the best solution.
LVL 13

Accepted Solution

LIONKING earned 500 total points
ID: 38737208
Check this out... It might give you another option:
LVL 13

Author Comment

ID: 38737417
That worked with some modifications. I'm hoping it generated a bulk insert command, I don't know how to test for that.  Here's the final code.

		Dim ProjectMain, ProjectSub As String
		ProjectMain = InputBox("Enter Main Project Name", "Main Project")
		If ProjectMain = "" Then Exit Sub
		ProjectSub = InputBox("Enter Sub Project Name", "Sub Project")
		If ProjectSub = "" Then Exit Sub

		Dim dr As DataRow
		Dim cnn As SqlConnection = Globals.cnnFACTS
		Using da As New SqlDataAdapter("SELECT * FROM tblTempUsed WHERE ID = -1", cnn)
			Using dt As New DataTable

				For Each r As DataGridViewRow In DataGridView1.Rows
					dr = dt.NewRow()
					With dr
						.Item("ProjectMain") = ProjectMain
						.Item("ProjectSub") = ProjectSub
						.Item("DiskNo") = r.Cells("DiskNo").Value
						.Item("BatchNo") = r.Cells("BatchNo").Value
					End With


				Dim sqlCB As New SqlCommandBuilder(da)

			End Using
		End Using

Open in new window

LVL 13

Expert Comment

ID: 38737504
Don't quote me on this, but I don't think it does a bulk insert.
What the data adapter does, is that it executes the InsertCommand that you specify for each row that is marked for insertion.

Glad it helped.

Featured Post

IT, Stop Being Called Into Every Meeting

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

16 Experts available now in Live!

Get 1:1 Help Now