Creating datatables

G

Guest

I would like to use a datatable to insert new records into a SQL Server database. I would like to create a datatable
that is the same structure as my SQL Server table. Since I am inserting new records into SQL Server I will not be doing
a select to retreive records into my datatable. Is there a way to retrieve the structure of my SQL Server table so tha
I can create an empty datatable? I can then insert my new records into this empty structure, without having to creat
the datatable manually. After populating the datatable I can use the update method to insert the new records into
SQL Server.
 
M

Miha Markic [MVP C#]

Hi Gary,

The best way would be to create table at design time.
If you need to create it at runtime then the fastest way would be to use
SqlCommands to query sql server using stored procedures such as sp_tables,
sp_columns, ...

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Gary said:
I would like to use a datatable to insert new records into a SQL Server
database. I would like to create a datatable
that is the same structure as my SQL Server table. Since I am inserting
new records into SQL Server I will not be doing
a select to retreive records into my datatable. Is there a way to
retrieve the structure of my SQL Server table so that
I can create an empty datatable? I can then insert my new records into
this empty structure, without having to create
the datatable manually. After populating the datatable I can use the
update method to insert the new records into
 
G

Guest

Miha

If using a stored procedure, should I use a select statement that would not retrieve any rows in the SQL table, but would
return the structure for creating the datatable. For example , If I know that customer 999999 does not exist in my table: SELECT customer_id, customer_address, cutomer_phone FROM tblCustomer WHERE customer_id = '999999' or is there another way to return the structure of the table from a stored procedure without any rows?
 
A

Andy Gaskell

SELECT customer_id, customer_address, cutomer_phone FROM tblCustomer WHERE 0
= 1

Won't return any rows

Gary said:
Miha,

If using a stored procedure, should I use a select statement that would
not retrieve any rows in the SQL table, but would
return the structure for creating the datatable. For example , If I know
that customer 999999 does not exist in my table: SELECT customer_id,
customer_address, cutomer_phone FROM tblCustomer WHERE customer_id =
'999999' or is there another way to return the structure of the table from a
stored procedure without any rows?
 
M

Miha Markic [MVP C#]

Hi Gary,

Why don't use sp_columns system procedure of Sql server?
It will verbosely return you metadata of chosen table(s).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Gary said:
Miha,

If using a stored procedure, should I use a select statement that would
not retrieve any rows in the SQL table, but would
return the structure for creating the datatable. For example , If I know
that customer 999999 does not exist in my table: SELECT customer_id,
customer_address, cutomer_phone FROM tblCustomer WHERE customer_id =
'999999' or is there another way to return the structure of the table from a
stored procedure without any rows?
 

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