{"id":2046,"date":"2012-08-22T16:01:44","date_gmt":"2012-08-22T21:01:44","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=2046"},"modified":"2012-08-22T16:01:44","modified_gmt":"2012-08-22T21:01:44","slug":"sql-server-example-of-a-c-stored-procedure","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2012\/08\/22\/sql-server-example-of-a-c-stored-procedure\/","title":{"rendered":"SQL Server example of a C# stored procedure"},"content":{"rendered":"<p>Below is a simple example of using CLR.<\/p>\n<p>Source code for procedure&#8230;<\/p>\n<pre lang=\"java\" line=\"1\">\r\nusing System.IO;\r\nusing Microsoft.SqlServer.Server;\r\n\r\npublic partial class spaceProc {\r\n  [Microsoft.SqlServer.Server.SqlProcedure]\r\n  public static void freeSpace() {\r\n    foreach (DriveInfo drive in DriveInfo.GetDrives()) { \r\n      if (drive.IsReady) {\r\n        SqlPipe p = SqlContext.Pipe; \r\n        p.Send(drive.Name + \" \" + drive.TotalFreeSpace \/ 1024 \/ 1024 + \"MB free, \"+ drive.TotalSize \/ 1024 \/ 1024 + \"MB total\");\r\n      }\r\n    } \r\n  }\r\n}\r\n<\/pre>\n<p>&#8230;and the command to create the library&#8230;<\/p>\n<pre lang=\"text\">\r\nC:\\Users\\showard>C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\csc.exe \/target:library getFree.cs\r\nMicrosoft (R) Visual C# 2010 Compiler version 2.0.50727\r\nCopyright (C) Microsoft Corporation. All rights reserved.\r\n\r\n\r\nC:\\Users\\showard>\r\n<\/pre>\n<p>&#8230;and SQL commands to create it&#8230;<\/p>\n<pre lang=\"sql\" line=\"1\">\r\nEXEC sp_configure 'clr enabled' , 1 \r\nRECONFIGURE \r\ngo\r\nALTER DATABASE test SET TRUSTWORTHY ON\r\ngo\r\nCREATE ASSEMBLY freeSpace from 'c:\\Users\\showard\\getFree.dll' WITH PERMISSION_SET = UNSAFE\r\ngo\r\nCREATE PROCEDURE getFreeSpace AS EXTERNAL NAME freeSpace.spaceProc.freeSpace\r\ngo\r\nexec getFreeSpace\r\n<\/pre>\n<p>&#8230;with the output below&#8230;<\/p>\n<pre lang=\"text\">\r\nC:\\ 21356MB free, 289782MB total\r\nD:\\ 0MB free, 4177MB total\r\n<\/pre>\n<p>You can also &#8220;sign&#8221; the assembly, which would be more secure, as what is above opens it up to everything.  This is just a quick and dirty way to get it up and running.  I may flag a follow-up for this in a few weeks to see if I can take it over the goal line with it totally secure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a simple example of using CLR. Source code for procedure&#8230; using System.IO; using Microsoft.SqlServer.Server; public partial class spaceProc { [Microsoft.SqlServer.Server.SqlProcedure] public static void freeSpace() { foreach (DriveInfo drive in DriveInfo.GetDrives()) { if (drive.IsReady) { SqlPipe p = SqlContext.Pipe;&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2012\/08\/22\/sql-server-example-of-a-c-stored-procedure\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,34],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2046"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2046"}],"version-history":[{"count":9,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2046\/revisions"}],"predecessor-version":[{"id":2463,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2046\/revisions\/2463"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2046"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}