Tutorial On Stored Procedures (For Use in VB)
Tutorial On Stored Procedures (For Use in VB)
Stored procedures are procedures that are stored within the database, works the same as commands, but is triggered by the program that needs to use it. Stored procedures normally requires a variable that needs to be passed in from the object using it for it to be able to execute SQL Functions normally. In this tutorial, I will create a table in the SQL database called tblTeam, and attempt to get the records out, and sorting them via the categoryID (last column). I will do so by using 4 objRanges and a stored procedure.
First off, create the table and insert values into the table by adding the codes above.
Secondly, create a new Visual Studios Project (Example, Excel) Once thats done, go to View > Server Explorer. You should end up with something like this.
After so, select your server name and your database name. Once thats done, choose OK. You should have something like this
Expand the folders, right click on Stored Procedures then add a new stored procedure.
This is the screen to create a new stored procedure. By default it will create the procedure with the name of dbo.StoredProcedure1, Change the name into something more appropriate by using a suitable prefix (usp_ , [user stored procedure]) After the name of the stored procedure, it will be codes for the variables. Variables that are used in these stored procedures will have a prefix of @. Let us do a very simple Select statement here. Since there are variables that are not constant (categoryID), we will need to use the @ variable here. Modify the codes in the stored procedure so that it looks like
*If you havent save the procedure yet Create Procedure usp_getTeamInfo @CategoryID int AS Select * from tblTeam where CategoryID = @CategoryId
*If you have already saved the procedure Alter Procedure usp_getTeamInfo @CategoryID int As Select * from tblTeamInfo where CategoryID = @CategoryId
Once thats done, we have our stored procedures already. Now, lets go back to the excel file and start using it! Go to Thisworkbook.vbs codes and enter the following:
Dim objCn As New SqlConnection Dim objCmd As New SqlCommand Dim objDa As New SqlDataAdapter Dim objDs As New DataSet Dim objDr As DataRow Dim i As Integer = 1 Dim objSheets As Excel.Worksheet Dim objRange1 As Excel.Range Dim objRange2 As Excel.Range Dim objRange3 As Excel.Range Dim objRangeCategory As Excel.Range
objSheets = Me.Sheets(1) objRangeCategory = objSheets.Cells(1, 1) objRangeCategory.Value = "Category 1" objRange1 = objSheets.Cells(2, 1) objRange2 = objSheets.Cells(2, 2) objRange3 = objSheets.Cells(2, 3) objCn.ConnectionString = CONNSTR objCmd.Connection = objCn objCn.Open() objCmd.CommandType = CommandType.StoredProcedure objCmd.CommandText = "usp_getTeamInfo" objDa.SelectCommand = objCmd
For Each objDr In objDs.Tables("Data").Rows objRange1.Value = objDr.Item("TeamName") objRange1 = objRange1.Offset(1, 0) objRange2.Value = objDr.Item("LeaderName") objRange2 = objRange2.Offset(1, 0) objRange3.Value = objDr.Item("Email") objRange3.Value = objRange3.Offset(1, 0) Next
The first chunk is for you to start calling variables to enable interaction with the database. The second chunk is for you to start assigning values to certain cells in the excel worksheet The third chunk is for you to start connecting to the database. The fourth chunk is to start the do until loop, because we want to separate the data by the categoryId, therefore the use of the do while loop. If a stored procedure is being used, the parameters must be added. In this case in the stored procedure weve already assigned a variable (@CategoryId), we must add it into the command. If no parameters are added but is still created in the stored procedure, an error will occur. After adding a parameter in, we need to assign the parameter a value. The value would be i, in the loop, i will automatically +1 after the rows have been displayed. After that, i will loop until it has reached the limit. While looping, the stored procedure will be called, and the data will be passed into the objDss make-believe table called Data. Then , it will be taken apart and assigned into the various objRanges as values. After the data has been taken apart and assigned, the for loop ends. After that, the rows are automatically moved 2 rows down, thus the offset(2,0). The if statement is to prepare the category heading only. It has no other particular use.