如何将数据集插入数据库? [英] How to insert a dataset into a database ?

查看:130
本文介绍了如何将数据集插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在互联网上搜索以使用OleDbAdapter将数据集插入到数据库中,但直到现在我一无所获.

谁能告诉我这样做的方法

PS:不想在插入时花很多时间遍历行,我需要Adapter.Update(DataSet1,"Items")之类的东西或接近它的东西,请:)

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 :)

推荐答案

http://support.microsoft.com/kb/301248 [ ^ ]
http://www.programmersheaven.com/mb/VBNET /304407/304407/inserting-a-dataset-into-a-database/ [
http://support.microsoft.com/kb/301248[^]
http://www.programmersheaven.com/mb/VBNET/304407/304407/inserting-a-dataset-into-a-database/[^]




看看这个问题的第一个答案:
http://stackoverflow.com/questions/1258637/insert-dataset-records-in-database [^ ]
答案是C#代码,这与在VB.NET中转换的代码相同:
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:
' 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


这篇关于如何将数据集插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆