New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

SQL Server: Triggers, Stored Procedures, and Functions
Illustration by

Writing stored procedures with C# .NET


From:

SQL Server: Triggers, Stored Procedures, and Functions

with Martin Guidry

Video: Writing stored procedures with C# .NET

Now we're going to write some C# code that will eventually become a stored procedure. We'll start off by create a new project in Visual Studio. For the project type I want it to be a Class Library. The result of a Class Library is a DLL and we will import that DLL into SQL Server. We'll name our Class Library CLRtest. Rather than use of the default class they have, I'll create a new class.
Expand all | Collapse all
  1. 2m 15s
    1. Welcome
      51s
    2. What you should know
      51s
    3. Using the exercise files
      33s
  2. 11m 1s
    1. Comparing triggers, functions, and procedures
      3m 25s
    2. Why use a stored procedure?
      4m 59s
    3. Why use functions?
      1m 27s
    4. Why use triggers?
      1m 10s
  3. 6m 2s
    1. Configuring your environment
      4m 53s
    2. Downloading and installing a sample database
      1m 9s
  4. 26m 25s
    1. Creating a stored procedure
      2m 46s
    2. Modifying a stored procedure
      2m 34s
    3. Returning data using data sets
      3m 45s
    4. Returning data using cursors
      3m 45s
    5. Using input and output parameters
      5m 24s
    6. Using security and permissions
      5m 24s
    7. Using transactions
      2m 47s
  5. 11m 56s
    1. Creating a user-defined function
      4m 59s
    2. Exploring single-value functions
      4m 18s
    3. Exploring table value functions
      2m 39s
  6. 9m 31s
    1. Using "after" triggers
      3m 47s
    2. Using "instead of" triggers
      2m 9s
    3. Using nested triggers
      1m 38s
    4. Using database-level triggers
      1m 57s
  7. 12m 43s
    1. Exploring a real-world INSERT procedure
      5m 32s
    2. Exploring a real-world UPDATE procedure
      3m 13s
    3. Implementing logging on DELETE
      3m 58s
  8. 19m 38s
    1. Understanding the Common Language Runtime (CLR) and the .NET framework
      1m 52s
    2. Using CLR with SQL Server 2012
      4m 11s
    3. Writing stored procedures with C# .NET
      5m 51s
    4. Writing functions with .NET
      5m 7s
    5. Choosing between T-SQL vs. CLR
      2m 37s
  9. 11m 34s
    1. Creating a basic web form and connecting to a database
      2m 56s
    2. Executing a stored procedure
      2m 4s
    3. Passing parameters
      3m 41s
    4. Getting return values
      2m 53s
  10. 1m 43s
    1. Next steps
      1m 43s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
SQL Server: Triggers, Stored Procedures, and Functions
1h 52m Advanced Sep 24, 2012

Viewers: in countries Watching now:

This course investigates several key database-programming concepts: triggers, stored procedures, functions, and .NET CLR (Common Language Runtime) assemblies. Author Martin Guidry shows how to combine these techniques and create a high-quality database using Microsoft SQL Server 2012. The course also covers real-world uses of the INSERT, UPDATE, and DELETE procedures, and how to build a basic web form to connect to your database.

Topics include:
  • Comparing triggers, functions, and stored procedures
  • Installing and configuring SQL Server
  • Creating a stored procedure
  • Returning data using data sets
  • Creating user-defined functions
  • Using "after," "instead," and nested triggers
  • Modifying existing stored procedures
  • Implementing logging on DELETE
  • Choosing between T-SQL and CLR
  • Executing a stored procedure
  • Passing parameters
Subjects:
Developer Databases
Software:
SQL Server
Author:
Martin Guidry

Writing stored procedures with C# .NET

Now we're going to write some C# code that will eventually become a stored procedure. We'll start off by create a new project in Visual Studio. For the project type I want it to be a Class Library. The result of a Class Library is a DLL and we will import that DLL into SQL Server. We'll name our Class Library CLRtest. Rather than use of the default class they have, I'll create a new class.

And I'll call the class mySprocs. The machine automatically wrote some code for me. Some of it I want, some of it I don't want, and I'll keep the using statements, but I'll also need to add a few of my own using statements, because we're working with a database, we will need to add using system.Data. We will also need to add using system. Data.SqlClient and system.Data.SqlTypes.

One more with the Microsoft.SqlServer.Server. So that should be all the using statements we need. I said I'd call myClass mySprocs. It'll need to be marked as a public class, and inside of that a class for now we're only going to have one method. The method will have to be marked as public and static. In this case it'll also be void, because it does not return anything.

The name will be InstertAuthor which is a descriptive name, because I plan to use this method to insert one record into the author's table. Before I build out the body of the method I'll need to decorate the method. The decoration sends information to the compiler on what we plan to do with this method. It will slightly change the behavior of the compiler. In this case we will decorate with the phrase Microsoft.SqlServer.Server.SqlProcedure, and most other places it's called a stored procedure and here it's called a SqlProcedure.

So this declaration will tell the compiler to compile this a little different, because it's going to be a SQL stored procedure. The body of the method will need a connection to the database. Typically, when creating a connection to a database, we have to go through a lot of effort to say the name of the server or the IP address of the server, the name of the database we want to connect to, the permissions, et cetera, et cetera. Here we can get off a lot easier, because remember this code is going to be running inside of a database. So we can get away with just saying connect to the database that we are part of already, and we say context connection=true.

In other words, use the connection that you are already part of the context of. Execute a single SQL statement, insert into authors. For this case we're just going to insert into two columns, FirstName and LastName. We'll insert the VALUE Sally, Smith. The next line, make sure that our command and our connection are talking to each other, and then we have three lines at the bottom, one to Open the connection one to ExecuteNonQuery. That's a NonQuery, because we're not expecting data to be returned and then we Close.

Obviously, best practice to close whatever you open. This looks like it's ready to go. We can go ahead and Build, and off the bottom we see a message saying build succeeded in the very lower left-hand corner. The only thing left to do now is to save this DLL. We're going to need to save it someplace that's easy to find. So I'll save it in the exercise files and then we will save it as CLRtest. Okay, so I should be done with Visual Studio.

Now I have to get into SQL Management Studio and I want to import that assembly. That will be a right-click > New Assembly, and I'll have to browse to the same directory I was just in, and there's my DLL. That looks successful. I now see the assembly CLRtest. So now that CLRtest has been imported, we can create a stored procedure based off of it.

Keyword CREATE PROC, and I'll need it to give it a name. I could name it anything I want, but it seems like the easiest thing would be to use the same name here as I used in the C# code which was InsertAuthor, and then I'll say AS EXTERNAL NAME. The first part is the name of the assembly, and then the name of a class, and then the name of the method and that ran successfully.

Good news, good news. We will look at our stored procedures, hit refresh and we see InsertAuthor. So great! Let's go ahead an execute that. And it says again completed successfully. Let's go ahead and look at the AuthorsTable and we would expect to see one record for Sally Smith, and there we go. The last line Sally Smith has now been inserted. So we've successfully wrote some C# code, compiled that C# code into a DLL, imported the DLL, made a stored procedure based on that DLL, and we can execute the stored procedure.

There are currently no FAQs about SQL Server: Triggers, Stored Procedures, and Functions.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server: Triggers, Stored Procedures, and Functions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.