using "Insert Into..." with dataAdapter Insert Command?

E

Ed

Hi,

I want to load data to a table in Sql Server from a
dataset table in my vb.net app using a dataAdapter. I
know how to do this as follows (my question is to see if I
can reduce the amount of code below):
....
Dim DA As SqlDataAdapter = New SqlDataAdapter
Dim Parm As New SqlParameter
....
DA.InsertCommand = New SqlCommand("Insert Into tbl1(fld0,
fld1, fld2) Values(@fld0, @fld1, @fld2)", conn)
Parm = DA.InsertCommand.Parameters.Add(New SqlParameter
("@fld0", NVarChar, 50, "fld0"))
Parm = sqlDA.InsertCommand.Parameters.Add(New SqlParameter
("@fld1", SqlDbType.NVarChar, 50, "fld1"))
Parm = sqlDA.InsertCommand.Parameters.Add(New SqlParameter
("@fld2", SqlDbType.NVarChar, 50, "fld2"))
....
DA.Update(dataset1, "tbl1")
....

The parameters is where I feel I am writing too much
code. Is there a way to say something like:

DA.InsertCommand = New SqlCommand("Insert Into tbl1 Select
* from " & dataTable.Name")

Could I create a class object that is a dataTable with a
property called "Name" or something like that? Or am I
limited to creating a bunch of parameters for the
dataAdapter Insert command? Or, is there a way to use the
Sql Command object without the dataAdapter to insert All
data to my sql server table from a dataset table (without
looping through a dataTable object)?

TIA,
Ed
 
E

Ed

Many thanks for your help! I could never figure out how
to use the commandbuilder - you have to use the
dataAdapter as an argument. Now I get it! Works great!

Many thanks,
Ed
 
H

Herfried K. Wagner [MVP]

* "Ed said:
I want to load data to a table in Sql Server from a
dataset table in my vb.net app using a dataAdapter. I
know how to do this as follows (my question is to see if I
can reduce the amount of code below):
...
Dim DA As SqlDataAdapter = New SqlDataAdapter
Dim Parm As New SqlParameter

Just FYI: There is a separate group for .NET + database (ADO.NET)
related questions avaliable:

<URL:
Web interface:

<URL:http://msdn.microsoft.com/newsgroups/?dg=microsoft.public.dotnet.framework.adonet>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top