Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Adding Rows to SQL Server table
 

Adding Rows to SQL Server table

What is the most efficient approach for adding rows to a SQL server table using VB.NET?

As a starting point I have included some code below which achieves the desired result, however, I am concerned it will result in a "memory hungry" application - since it calls the DataAdapter.Fill Method which requires the application to hold the entire table in memory prior to adding the row (correct me if I am wrong).

Can anyone provide examples which achieve the same result but with less overhead?

Any advice is much appreciated. Thanks for your time.

Dim strSelect As String

Dim cn As SqlConnection = New SqlConnection(My.Settings.MyConnectionString)
cn.Open()

strSelect = "SELECT * FROM MyTable"

'DataAdapter
Dim daMyTable As SqlDataAdapter = New SqlDataAdapter(strSelect, cn)

'Auto Generate UPDATE, INSERT, and DELETE commands based on SELECT command
Dim autogenMyTable As New SqlCommandBuilder(daMyTable)

'DataSet
Dim dsMyTable As DataSet = New DataSet()

'Fill DataSet
daMyTable.Fill(dsMyTable, "MyTable")

'Temporary DataTable
Dim dtMyTable As DataTable = dsMyTable.Tables("MyTable")

Dim rowMyTable As DataRow

'Add a record.
rowMyTable = dtMyTable.NewRow()
rowMyTable("Field1") = "Test Data"

dtMyTable.Rows.Add(rowMyTable)

'Update the database.
daMyTable.Update(dsMyTable, "MyTable")

cn.Close()
msgjunkie  Tuesday, October 06, 2009 12:50 PM
You can simplify this process by simply executing an Insert command against the database. Then you would not need to retrieve the data before you insert it.

I have some sample code here:

http://msmvps.com/blogs/deborahk/archive/2009/08/21/dal-save-data-using-a-sql-statement.aspx

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer bymsgjunkie Wednesday, October 07, 2009 9:54 AM
  •  
DeborahK  Tuesday, October 06, 2009 6:57 PM
You can simplify this process by simply executing an Insert command against the database. Then you would not need to retrieve the data before you insert it.

I have some sample code here:

http://msmvps.com/blogs/deborahk/archive/2009/08/21/dal-save-data-using-a-sql-statement.aspx

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Marked As Answer bymsgjunkie Wednesday, October 07, 2009 9:54 AM
  •  
DeborahK  Tuesday, October 06, 2009 6:57 PM
Thanks DeborahK, the sample code you have provided is presented very clearly and works perfectly.
msgjunkie  Wednesday, October 07, 2009 9:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Extended Data Grid
• Binding to object datasource
• DataGridView columns in designer
• Changing Custom Control Property
• datagrid column period-delimited hierarchy binding
• comma-separated file pasted into datagridview
• HOW EXCEL GOES INTO DATABASE(MAPPEING)
• Datagridview-Scrollbar
• cell validation
• DataGridView Cell Formatting