Click on the banner to learn about and purchase my database training on Azure

SQL Server - How to read, list, create, change and delete Windows registry keys (Regedit) by CLR (C #)

Views: 993 views
Reading Time: 6 minutes

Hello people,
Good afternoon!

In today's post, I will demonstrate how to manage (read, list, create, change, and delete) Windows registry keys through the database using SQL CLR and the C # programming language (CSharp), which allows us to greatly extend the capabilities and functionality of the SQL Server database.

This kind of functionality is very useful when you need to quickly look up some information in a server's Windows registry or make a registry change without connecting to the server, just performing a Stored Procedure, or even applying a registry change to multiple machines. or servers in an automated manner.

Remember that I had already made a post (SQL Server - Querying instance information in Windows Registry using sys.dm_server_registry and xp_instance_regread) where it was possible to read some information from the Windows registry without using the CLR, but relating only to SQL Server instance information and not the general reading of any registry key let alone manipulating that information, which motivated me to create this new post for this need.

Some Stored Procedures below require the use of the Return class, which I use to send CLR alert and / or error messages to the database. The source code of this class can be viewed in the post. SQL Server - How to send warnings and error messages to the bank through CLR (C #)

How to read and list Windows registry keys

How to read and list Windows registry keys

sql-server-how-to-list-windows-registry-regedit-sql-clr

To list the Windows records, I will use the RegistryRow class, which encapsulates various methods of the Microsoft.Win32.RegistryKey library and I use it in the table-valued fncRegEdit_Listar function, as in the example above, where I enter as parameters the machine name and key which will be read.

RegEdit.cs

fncRegEdit_Listar.cs

How to Create Windows Registry Folders

How to Create Windows Registry Folders

sql-server-how-to-list-windows-registry-regedit-sql-clr-2

sql-server-how-to-list-windows-registry-regedit-sql-clr-3

To create folders in Windows registries, I use the stpRegEdit_Pasta_Criar Stored Procedure, as in the example above, which allows you to create directories under a Windows registry key.

stpRegEdit_Pasta_Criar.cs

How To Delete Windows Registry Folders

How To Delete Windows Registry Folders

sql-server-how-to-list-windows-registry-regedit-sql-clr-4

To delete folders in the windows registry, I use the stpRegEdit_Pasta_Apagar Stored Procedure, as in the example above, which allows you to delete directories from the windows registry. The @Fl_Recursive = 1 binary flag allows you to recursively delete keys and sub directories from a given directory.

If the @Fl_Recursivo = 0 flag, it will only be possible to delete directories that do not have no sub-directories and if you try, you will encounter the error message “System.ApplicationException: Error: Registry key has subkeys and recursive removes are not supported by this method. ”.

stpRegEdit_Pasta_Apagar.cs

How to create / change Windows registry keys

How to create / change Windows registry keys

How to create keys and values ​​in the Windows registry:
sql-server-how-to-list-windows-registry-regedit-sql-clr-5

How to change keys and values ​​in the Windows registry:
sql-server-how-to-list-windows-registry-regedit-sql-clr-6

To create / change Windows registries, I use the stpRegEdit_Chave_Criar Stored Procedure as exemplified above and the source code is available below:

stpRegEdit_Chave_Criar.cs

How To Delete Windows Registry Keys

How To Delete Windows Registry Keys

sql-server-how-to-list-windows-registry-regedit-sql-clr-7

To delete Windows registry keys, I use the Stored Procedure stpRegEdit_Chave_Apagar, in the same patterns as the Stored Procedures above.

stpRegEdit_Chave_Apagar.cs

If you didn't know what CLR is and would like to know more about it, visit my post. Introduction to SQL Common Language Runtime (CLR) in SQL Server.

That's it folks!
Until the next post.

sql server C # csharp access access list list view view read windows registry registry regedit

sql server C # csharp access access list list view view read windows registry registry regedit