Created
February 24, 2011 20:02
-
-
Save zippy1981/842778 to your computer and use it in GitHub Desktop.
T-SQL Script that adds CLR UDFs and Stored procs which do .NET config file editing in SQL server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| USE [master] | |
| GO | |
| IF EXISTS (SELECT name FROM sys.databases WHERE name = N'TestAssembly') | |
| BEGIN | |
| ALTER DATABASE [TestAssembly] SET OFFLINE WITH ROLLBACK IMMEDIATE; | |
| ALTER DATABASE [TestAssembly] SET ONLINE; | |
| DROP DATABASE [TestAssembly]; | |
| END | |
| GO | |
| CREATE DATABASE TestAssembly | |
| GO | |
| USE TestAssembly | |
| GO | |
| ALTER DATABASE TestAssembly SET TRUSTWORTHY ON; | |
| GO | |
| ALTER AUTHORIZATION ON DATABASE::TestAssembly TO test | |
| GO | |
| CREATE ASSEMBLY LoggedClr | |
| from 'C:\justin''s projects\TestClr\LoggedClr\LoggedClr\bin\Debug\LoggedClr.dll' | |
| WITH PERMISSION_SET = EXTERNAL_ACCESS | |
| GO | |
| CREATE FUNCTION GetConfigFileName () RETURNS NVARCHAR(MAX) AS | |
| EXTERNAL NAME LoggedClr.[LoggedClr.AppDomainInfo].GetConfigFileName | |
| GO | |
| CREATE FUNCTION GetAppConfigValue (@key nvarchar(max)) RETURNS nvarchar(max) AS | |
| EXTERNAL NAME LoggedClr.[LoggedClr.AppDomainInfo].GetAppConfigValue | |
| GO | |
| CREATE PROCEDURE SetAppConfigValue (@key nvarchar(max), @value nvarchar(max)) AS | |
| EXTERNAL NAME LoggedClr.[LoggedClr.AppDomainInfo].SetAppConfigValue | |
| GO | |
| SELECT dbo.GetConfigFileName() | |
| EXEC dbo.SetAppConfigValue 'justin', 'is a developer' | |
| SELECT dbo.GetAppConfigValue('justin') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Configuration; | |
| using Microsoft.SqlServer.Server; | |
| namespace LoggedClr | |
| { | |
| public static class AppDomainInfo | |
| { | |
| [SqlFunction] | |
| public static string GetConfigFileName() | |
| { | |
| return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; | |
| } | |
| [SqlFunction] | |
| public static string GetAppConfigValue(string key) | |
| { | |
| return ConfigurationManager.AppSettings[key]; | |
| } | |
| [SqlProcedure] | |
| public static void SetAppConfigValue(string key, string value) | |
| { | |
| ConfigurationManager.AppSettings[key] = value; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment