|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 4:00 AM
Points: 533,
Visits: 2,285
|
|
I'd be interested to see if anyone has a solution to the problem of preventing a barf when an invalid path is passed to DirectoryInfo. The obvious idea would be to send a RAISERROR through the Pipe to the SQLExecutionContext via ExecuteAndSend(). The error should be caught by the outer exception handling of the TSQL TRY....CATCH block so you can then do something sensible. However, in SQL Server 2005, the error is thrown back to the CLR and handled as SQLExpection. If the SQLException is caught within the CLR and nothing is done within the CLR CATCH block, then the execution will fail in the T-SQL context with an error, instead of being caught by the TSQL-TRY/CATCH Block. (Jens Suessmeyer in http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=251376)
Best wishes,
Phil Factor Simple Talk
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:39 PM
Points: 6,260,
Visits: 1,977
|
|
Although SQLCLR has great power I think it is very important to tell the reader that it also brings MANY undesirable things in. - Bad Exception handling is just one of the examples. - Bad routines could cause stability problems even knowing that is a "hosted" CLR - There is a very high risk of introducing code that is not Server-Side friendly. - Problems caused by CLR objects are a lot harder to deal with.
* Noel
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 7:27 AM
Points: 1,529,
Visits: 912
|
|
| Last time I worked on a CLR function for SQL Server, I was unable to find a way to handle an exception and pass an error back through the pipe to SQL Server. Now this was a scalar function versus a table value function, but I'm not sure you can handle the error a pass an exception back through the pipe to SQL Server on in a CLR function. You may be stuck with getting the .NET exception back in TSQL. You can do handle the exception and pass an error back through the pipe in a CLR Stored Procedure, however.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 4:26 PM
Points: 1,696,
Visits: 1,742
|
|
Phil Factor (2/5/2009) I'd be interested to see if anyone has a solution to the problem of preventing a barf when an invalid path is passed to DirectoryInfo. The obvious idea would be to send a RAISERROR through the Pipe to the SQLExecutionContext via ExecuteAndSend(). The error should be caught by the outer exception handling of the TSQL TRY....CATCH block so you can then do something sensible. However, in SQL Server 2005, the error is thrown back to the CLR and handled as SQLExpection. If the SQLException is caught within the CLR and nothing is done within the CLR CATCH block, then the execution will fail in the T-SQL context with an error, instead of being caught by the TSQL-TRY/CATCH Block. (Jens Suessmeyer in http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=251376)
The clean solution is to return null if the existence check fails:
public static IEnumerable os_directory_info(SqlString path, SqlString filter) { DirectoryInfo di = new DirectoryInfo(path.Value); if (di.Exists) { if (filter.IsNull || filter.Value == string.Empty) return di.GetFileSystemInfos(); else return di.GetFileSystemInfos(filter.Value); } else { return null; } }
Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008 My Blog | Twitter | MVP Profile Training | Consulting | Become a SQLskills Insider Troubleshooting SQL Server: A Guide for Accidental DBAs
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 4:26 PM
Points: 1,696,
Visits: 1,742
|
|
noeld (2/5/2009) Although SQLCLR has great power I think it is very important to tell the reader that it also brings MANY undesirable things in. - Bad Exception handling is just one of the examples.
Actually .NET has very robust exception handling abilities, and if you implement them along with TSQL exception handling there shouldn't be problems.
BEGIN TRY
select * from dbo.os_directory_info('f:\Start', null)
END TRY BEGIN CATCH
SELECT error_message()
END CATCH
- Bad routines could cause stability problems even knowing that is a "hosted" CLR
Describe a stability problem that you have encountered in a SAFE/EXTERNAL_ACCESS assembly implementation? If you go UNSAFE all bets are off, but SAFE/EXTERNAL_ACCESS can not cause you stability issues.
- There is a very high risk of introducing code that is not Server-Side friendly.
You also have to run this kind of code with UNSAFE access, so it really is all about how you control what you put into your SQL Server.
- Problems caused by CLR objects are a lot harder to deal with.
Describe a problem caused by a SAFE/EXTERNAL_ACCESS assembly in SQL Server and how it was harder to deal with.
Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008 My Blog | Twitter | MVP Profile Training | Consulting | Become a SQLskills Insider Troubleshooting SQL Server: A Guide for Accidental DBAs
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 4:26 PM
Points: 1,696,
Visits: 1,742
|
|
Phil Factor (2/5/2009)
use of CLR makes it harder for a SQL Server administrator to see what's going on with their SQL Server. Yes, Agreed! However, I've recently started using NET Reflector with an add-in that allows you to see the source of all the CLR routines currently in the database. http://www.denisbauer.com/NETTools/SQL2005Browser.aspx It is a joy to use and it means that the D**ned developers can't hide their code from you, particularly as you can decompile it into VB or C#. and save the source to a file. Power to the DBA!
Depending on how you deploy your Assembly you don't really even need Reflector. For brevities sake I didn't include all the files in the script I provided here, but if you do, then you can get the code from the sys.assembly_files DMV:
declare @content varchar(max)
select @content= cast(content as varchar(max)) from sys.assembly_files where name = 'os_directory_info.cs'
print @content
Jonathan Kehayias | Principal Consultant | MCM: SQL Server 2008 My Blog | Twitter | MVP Profile Training | Consulting | Become a SQLskills Insider Troubleshooting SQL Server: A Guide for Accidental DBAs
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 4:26 PM
Points: 1,696,
Visits: 1,742
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 7:27 AM
Points: 1,529,
Visits: 912
|
|
Thanks for the article. The last time I was developing a CLR function/procedure I could not find a good explanation of how to handle the signing and install of the assembly for external access. So, we had to use the trustworthy setting. Now I have a better idea of the key install.
Thanks
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, July 07, 2009 1:52 PM
Points: 1,
Visits: 7
|
|
The reason's that most DBA's continue to use xp_cmdshell is simple - it's easier. There are numerous examples on the web of how to get a directory listing using xm_cmdshell. I cut and paste, and voila! I have my routine. Using SQLCLR requires someone to know a different language, and all it's data access routines (Fill, etc.). So most DBA's will stick with what they know and since everyone's overworked, will not use something new.
BTW, I write and use SQLCLR for special mathematical functions that we write, so I have nothing against it. But I've been writing C/C++/Vb code since the 80's, and because I'm familiar with the algol-68 derivative languages, it's easy for me to switch back and forth. I don't think you can expect that of most people.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 4:00 AM
Points: 533,
Visits: 2,285
|
|
Oooh. This is getting very useful. I'd missed out on the advantages of passing string parameters as SQLStrings and the trick of passing back a NULL. It makes perfect sense. Now it has the behaviour that one would want.
Best wishes,
Phil Factor Simple Talk
|
|
|
|