When creating managed code that will reside within SQL Server as a stored procedure, you are basically creating a static method on a class. That static method is then decorated with the Microsoft.SqlServer.Server.SqlProcedure attribute.
When your assembly is deployed to SQL Server and stored within the database, this attribute allows SQL to create a CLR routine for the method.
Whenever you add a new stored procedure to your SQL Server project, it will be
part of the partial class StoredProcedures and the static
method representing the procedure will be in its own file.
By default, SQL Server 2005 doesn't allow you to execute CLR
code, so you'll have to enable it by executing the following command inside a
SQL query window (make sure you're connected with sufficient privileges to
perform this command):
sp_configure 'clr enable', 1
After executing this, SQL Server will inform you that the
option has changed, but it will not take effect until you issue the following
command:
Now, here is the code of our CLR stored procedure:
using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;
public partial class StoredProcedures{
[Microsoft.SqlServer.Server.SqlProcedure]public static void MyCLRStoredProcedure(out SqlString outVal)
{
outVal = "Hello World";
}
};
When building C# static methods that will be used as stored
procedures, you need to remember the following rules:
-
The return type of the method is used as the return value of
the stored procedure or function.
-
The parameter list of the method is the parameter list of the
stored procedure. As such, you should only use data types from the
System.Data.SqlTypes namespace.
-
Keep in mind that your method has no user interface, so any
debugging or tracing you do can't go to a console window. You can still print
debug messages the same way you could with stored procedures,
however.
When writing stored procedures, types, functions, and more, you
will need access to data and functionality provided by
SQL
Server. For example, when writing a C# stored procedure you need to be able to
send results out on the "pipe." Also, you will want to be able to run your
queries using the existing server-side connection and not have to resort to a
client-side ADO.NET connection. All of this is available for you in a few
classes, such as
SqlContext and
SqlPipe.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void RadarScan(int UserID)
{
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText =
"SELECT FitstName, LastName FROM Contacts WHERE ID = " + UserID.ToString();
//SqlDataReader rdr = cmd.ExecuteReader();
//if (rdr.Read())
//{
//}
SqlContext.Pipe.ExecuteAndSend(scanCmd);
rdr.Close();
} // no need to explicitly shut down connection because of 'using' statement
}
};