Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,800 courses, including more Business and personalized recommendations.Start Your Free Trial Now
- View Offline
- 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
Skill Level Advanced
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.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.