VP - Lab 14
VP - Lab 14
VP - Lab 14
Visual Programming
Lab 14
Objectives
Although ‘Insert’ query can be used multiple times to add bulk of records in database, however,
.Net environment and Microsoft SQL Server provide a better and effective approach for this.
It includes creating a stored procedure that is executed for inserting records; and a User Defined
Table Type that can be used as a parameter to stored procedure. This User Defined Table Type
can hold a DataTable which makes it easy to insert a while DataTable at once in database.
For any table in your database, create a User Defined Type Table including columns for which
you want to add values. (They may or may not be all columns).
In your database, go to Programmability > Types > User-Defined Table Type > User-Defined
Table Type
Use the following sample script to add a User-Defined Table Type. Make changes as needed:
Now, we have to create a stored procedure that uses the previously created User Defined Table
Type, in order to insert records.
Use the following script to create your stored procedure. Make changes where required.
Notice that previously created User Defined Table Type (UD_Inventory) is being used here to
insert records in Inventory table. All records from UD_Inventory will be added to Inventory
table.
Before moving to Visual Studio for programming our application, we may test if the stored
procedure is running fine.
As our stored procedure takes a table as parameter, we will create one and pass it to the stored
procedure to check.
Use the following script to check your stored procedure. Make changes where required.
We will be using the following function DBAccess function to execute stored procedures.
Notice that we are simply using ExecuteNonQuery() function that was already used for insertion.
Moreover, command type has been modified to StoredProcedure in this case.
We just have to pass a paramter to our command, which is a datatable that will be replaced for
@InventoryTable defined in the stored procedure.
Pro Tips:
The data table can be created with any approach; getting data from a grid view or even
textboxes or other controls.