Insert into Table from Manually Built DataTable

O

og

Hello All,
I have an application where I manually build two DataTables (i.e., I don't
query a database table to populate the DataTables). I have to do this
manual creation of the DataTables. These DataTables have the same structure
as their associated database tables.

Once I have the manual DataTables created with all the required data, I'd
like to then insert the entire DataTable into its associated database table,
without having to loop through each record of the DataTable and perform an
insert for each row. Does that make sense?

Would anyone know if it's possible to take a manually built DataTable and
insert all the data from it into a database table in one swoop? And, if so,
how?

Thanks.
 
M

Miha Markic [MVP C#]

Hi,

errr, DataTables are always built manually, well, sort of. DataAdapter can
build one for you but such DataTable is not different from one built
manually.
So, you would need a properly configured dataadapter for storing data from a
DataTable to database and if we are talking .net 1.1 and sql server, there
is batch update support. The batch update support for sql server is comming
with .net 2.0.
 
O

og

Hello, and thanks for the reply.

When a say built manually, what I'm referring to is something like:

DataTable dt = new DataTable;
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
DataRow dr = dt.NewRow();
dr["col1"] = "row1";
dr["col2"] = "row2";
dr["col3"] = "row3";

which I'm sure you already know. But, the point is that I'm not using a
SqlDataAdapter to build my DataTable.

Also, I'm using VS2005 (.NET 2.0) and SQL Server 2005--all beta versions, of
course.

So, is there some sort of batch method to put the whole manually built
DataTable into the database table in one pass? Or, am I forced to iterate
thru each row in the manually built DataTable and perform a separate insert
statement for each row?

Thanks for the help.




Miha Markic said:
Hi,

errr, DataTables are always built manually, well, sort of. DataAdapter can
build one for you but such DataTable is not different from one built
manually.
So, you would need a properly configured dataadapter for storing data from a
DataTable to database and if we are talking .net 1.1 and sql server, there
is batch update support. The batch update support for sql server is comming
with .net 2.0.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

og said:
Hello All,
I have an application where I manually build two DataTables (i.e., I don't
query a database table to populate the DataTables). I have to do this
manual creation of the DataTables. These DataTables have the same
structure
as their associated database tables.

Once I have the manual DataTables created with all the required data, I'd
like to then insert the entire DataTable into its associated database
table,
without having to loop through each record of the DataTable and perform an
insert for each row. Does that make sense?

Would anyone know if it's possible to take a manually built DataTable and
insert all the data from it into a database table in one swoop? And, if
so,
how?

Thanks.
 
M

Miha Markic [MVP C#]

Hi,

You should use SqlDataAdapter to store the records - it is exactly what are
you looking for.
It doesn't matter how the DataTable was built.
Check out the help files about SqlDataAdapter class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info


og said:
Hello, and thanks for the reply.

When a say built manually, what I'm referring to is something like:

DataTable dt = new DataTable;
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
DataRow dr = dt.NewRow();
dr["col1"] = "row1";
dr["col2"] = "row2";
dr["col3"] = "row3";

which I'm sure you already know. But, the point is that I'm not using a
SqlDataAdapter to build my DataTable.

Also, I'm using VS2005 (.NET 2.0) and SQL Server 2005--all beta versions,
of
course.

So, is there some sort of batch method to put the whole manually built
DataTable into the database table in one pass? Or, am I forced to iterate
thru each row in the manually built DataTable and perform a separate
insert
statement for each row?

Thanks for the help.




Miha Markic said:
Hi,

errr, DataTables are always built manually, well, sort of. DataAdapter
can
build one for you but such DataTable is not different from one built
manually.
So, you would need a properly configured dataadapter for storing data
from a
DataTable to database and if we are talking .net 1.1 and sql server,
there
is batch update support. The batch update support for sql server is comming
with .net 2.0.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

og said:
Hello All,
I have an application where I manually build two DataTables (i.e., I don't
query a database table to populate the DataTables). I have to do this
manual creation of the DataTables. These DataTables have the same
structure
as their associated database tables.

Once I have the manual DataTables created with all the required data, I'd
like to then insert the entire DataTable into its associated database
table,
without having to loop through each record of the DataTable and perform an
insert for each row. Does that make sense?

Would anyone know if it's possible to take a manually built DataTable and
insert all the data from it into a database table in one swoop? And,
if
so,
how?

Thanks.
 

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