Download - SQLCLR Tips & Trics
![Page 1: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/1.jpg)
![Page 2: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/2.jpg)
SQLCLR TIPS&TRICKS
Daniel Joskovski
Owner Omnis llc, Principal Developer Daenet,
MCT Semos [email protected]
![Page 3: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/3.jpg)
![Page 4: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/4.jpg)
![Page 5: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/5.jpg)
About me
![Page 6: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/6.jpg)
Agenda
Why SQL CLR?
What Is Assembly and How To Get Info About?
Demo1
Create CLR Functions
Demo 2
Discussion
Encrypt/Decrypt
![Page 7: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/7.jpg)
Why SQL CLR?
A better programming model
Improved safety and security
Ability to define data types and aggregate functions
Streamlined development through a standardized environment
Potential for improved performance and scalability
![Page 8: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/8.jpg)
What Is Assembly
Assemblies are DLL files used in an instance of SQL Server to deploy:
Functions
Stored procedures
Triggers
User-defined aggregates
User-defined types
![Page 9: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/9.jpg)
Getting Info About Assemblies ASSEMBLYPROPERTY('assembly_name', 'property_name')
![Page 10: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/10.jpg)
• sys.assemblies
![Page 11: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/11.jpg)
• sys.assembly_files
![Page 12: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/12.jpg)
• sys.assembly_modules
![Page 13: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/13.jpg)
Demo 1
Getting Info About Assemblies
![Page 14: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/14.jpg)
SQL Server In-Process Specific Extensions to ADO.NET
SqlContext Object This class provides access to the other extensions by abstracting the context of a caller of a SQL Server routine that executes managed code in-process.
SqlPipe Object This class contains routines to send tabular results and messages to the client.
SqlDataRecord Object The SqlDataRecord class represents a single row of data, along with its related metadata, and allows stored procedures to return custom result sets to the client.
![Page 15: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/15.jpg)
• SqlTriggerContext Object This class provides information on the context in which a trigger is run.
using System; using System.Data; using System.Data.Sql; using Microsoft.SqlServer.Server; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Xml; using System.Text.RegularExpressions;
public class CLRTriggers{ public static void DropTableTrigger()
{ SqlTriggerContext triggContext = SqlContext.TriggerContext;switch(triggContext.TriggerAction)
{ case TriggerAction.DropTable:SqlContext.Pipe.Send("Table dropped! Here's the EventData:"); SqlContext.Pipe.Send(triggContext.EventData.Value); break; default: SqlContext.Pipe.Send("Something happened! Here's the EventData:"); SqlContext.Pipe.Send(triggContext.EventData.Value);break;}
}}
![Page 16: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/16.jpg)
Returning Custom Result Set
Managed stored procedures can send result sets that do not come from a SqlDataReader. The SendResultsStart method, along with SendResultsRow and SendResultsEnd, allows stored procedures to send custom result sets to the client.
SendResultsStart takes a SqlDataRecord as an input. It marks the beginning of a result set and uses the record metadata to construct the metadata that describes the result set. It does not send the value of the record with SendResultsStart. All the subsequent rows, sent using SendResultsRow, must match that metadata definition.
![Page 17: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/17.jpg)
DEMO 2
Creating Managed Objects with Visual Studio
![Page 18: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/18.jpg)
public static void TransactionHistoryRunningSum()
{
using (SqlConnection conn = new SqlConnection("context connection=true;"))
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = @"" +
"SELECT TransactionID, ActualCost " +
"FROM Production.TransactionHistory " +
"ORDER BY TransactionID";
SqlMetaData[] columns = new SqlMetaData[3];
columns[0] = new SqlMetaData("TransactionID", SqlDbType.Int);
columns[1] = new SqlMetaData("ActualCost", SqlDbType.Money);
columns[2] = new SqlMetaData("RunningTotal", SqlDbType.Money);
decimal RunningSum = 0;
SqlDataRecord record = new SqlDataRecord(columns);
SqlContext.Pipe.SendResultsStart(record);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
decimal ActualCost = (decimal)reader[1];
RunningSum += ActualCost;
record.SetInt32(0, (int)reader[0]);
record.SetDecimal(1, ActualCost);
record.SetDecimal(2, RunningSum);
SqlContext.Pipe.SendResultsRow(record);
}
SqlContext.Pipe.SendResultsEnd();
}
}
![Page 19: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/19.jpg)
Discusion
Other way?
Ideas?
![Page 20: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/20.jpg)
Supported Assemblies
Microsoft.Visualbasic.dll
Mscorlib.dll
System.Data.dll
System.dll
System.Xml.dll
Microsoft.Visualc.dll
Custommarshallers.dll
System.Security.dll
System.Web.Services.dll
System.Data.SqlXml.dll.
![Page 21: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/21.jpg)
Demo
Creating PDF document in SQLCLR using IText
![Page 22: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/22.jpg)
Documentation used
http://msdn.microsoft.com/en-us/library/ms131102.aspx
http://msdn.microsoft.com/en-us/library/ms131075.aspx
http://msdn.microsoft.com/en-us/library/ms131070.aspx
![Page 23: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/23.jpg)
• Complete electronic evaluation forms on the computers in the hall and enter to win!– Infragistics Ultimate
– Telerik DevCraft
– JetBrains .NET tools
– Semos training vouchers
– Pluralsight subscriptions
– and many more…
![Page 24: SQLCLR Tips & Trics](https://reader034.vdocument.in/reader034/viewer/2022052622/55957fda1a28abce318b45db/html5/thumbnails/24.jpg)