Click here to Skip to main content
15,886,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I Searched the internet to use the OleDbAdapter to insert a dataset into my database but until now i came out with nothing.

can anyone please tell me the way to do it

PS: Don't want to spend lots of time looping through the rows while inserting, i need something like Adapter.Update(DataSet1,"Items") or something close to it, please :)
Posted
Updated 18-Nov-12 3:19am
v2
Comments
m a suresh 15-Jun-16 5:44am    
how to insert rows into a database table from a dataset ?
Please help..

Hi,

Have a look at the first answer of this question:
http://stackoverflow.com/questions/1258637/insert-dataset-records-in-database[^]
The answer is C# code, this is the same code converted in VB.NET:
VB
' Prerequisite: The data to be inserted is available in a DataTable/DataSet.
Dim data = New DataTable()
data.Columns.Add("CompanyName", GetType(String))
data.Columns.Add("Phone", GetType(String))
data.Rows.Add("Foo", "12345678")
data.Rows.Add("Bar", "87654321")

' Now, open a database connection using the Microsoft.Jet.OLEDB provider.
' The "using" statement ensures that the connection is closed no matter what.
Using connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=Northwind.mdb")
	connection.Open()

	' Create an OleDbDataAdapter and provide it with an INSERT command.
	Dim adapter = New OleDbDataAdapter()
	adapter.InsertCommand = New OleDbCommand("INSERT INTO Shippers (CompanyName, Phone) VALUES (@CompanyName , @Phone)", connection)
	adapter.InsertCommand.Parameters.Add("CompanyName", OleDbType.VarChar, 40, "CompanyName")
	adapter.InsertCommand.Parameters.Add("Phone", OleDbType.VarChar, 24, "Phone")

	' Hit the big red button!
	adapter.Update(data)
End Using
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900