Save C# DataTable to Sql Server Table - CodAffection
[the_ad_placement id="before-content"]

Save C# DataTable to Sql Server Table

How to save c# table into sql server table


In this article, I would like to explain the fastest way to save c# datatable to sql server table using user defined table type.

Download project from :


product table definition

I have created a demo table product.

In c# project,we have a data table populated with some test data.

product table with data

Now we need to insert this c# table into sql server product table.

Old Way

if you don’t know this new way of insertion. you will do something like below

there is no wrong in doing this. but insertion of  tables with more rows will affect your application perfomance.

Best Practice

Step 1 : create user defined table

prefix udt indicate user defined table.

Step 2 : create stored procedure with user defined type

Step 3 : execute stored procedure from c#

sqlcon : sql connection object
productTable : c# dataTable which is to be inserted

How to alter user defined table ?

There is no command to alter or modify user defined type in sql need to drop and recreate the type.

But in management studio we can recreate type using script.
1.generate script for the type.(in new query window or as a file)

2.delete user defied table.
3.modify the create script and then execute.

Download project from :

video tutorial : watch from here

Scroll to Top
Share via
Copy link
Powered by Social Snap