Next step of programming

Just another WordPress.com weblog

Archive for the ‘ADO.NET’ Category

CLR String TitleCase Function in SQL Server 2005 (CLR Integration)

without comments

With new feature of CLR integration we can provide function with in our assemblies which can be accessed by the user in the TSQL statements. Let us now try to create one title case function. First create one library project in visual studio 2005. Then create one class with one function inside that.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

namespace AssemblyFunctions
{

public partial class MySQLFunctions    
{
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static string TitleCase(string data)
{
string caseData = data.ToString().Substring(0, 1).ToUpper()
+ data.ToString().Substring(1, data.ToString().Length -1).ToLower();

return caseData;
}
}
}

Now we can compile this assembly and keep it at one location. Then move to MSSQL Management Studio and there in query window. execute following commands one bye one
First we need to enable the CLR integration for that we have to use the sp_configure store procedure in the following way

--Configure the clr enabled state
sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO

Now to access the assembly into our MSSQL Server we have to register it first

--REGISTER CLASS LIBARIES IN SQL SERVER
CREATE ASSEMBLY ASSEMBLYFUNCTIONS 
FROM 'E:\personal\projects\AssemblyFunctions\AssemblyFunctions\bin\Debug\AssemblyFunctions.dll'
WITH PERMISSION_SET = SAFE
GO

To drop that assembly use the following commands

--DROP/UN-REGISTER THE ASSEMBLY 
DROP ASSEMBLY ASSEMBLYFUNCTIONS
GO

Now to access our functions we have to first register our functions, procedures or what ever is there

--REGISTER USER DEFINED FUNCTION IN SQL SERVER 
CREATE FUNCTION TITLECASE(@DATA NVARCHAR(MAX)) RETURNS NVARCHAR(MAX)
AS EXTERNAL NAME AssemblyFunctions.[AssemblyFunctions.MySQLFunctions].TitleCase  GO

Since now all the things are done you can call your respective function

Select dbo.TitleCase('sample')
go


Also this assembly and function will be added to the mssql server. see the image below

CLR Integration

CLR Integration

Written by A.Sethi

December 12, 2008 at 11:02 am

Listing SQL Server Instances running in network with details C#

without comments

To list the server running in the listbox or the grid we can use following code and give user a option to select the server to whom he wants to connect

using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;

namespace ListSQLServer
{
public partial class serverListing : Form
{
public serverListing()
{
InitializeComponent();
}


private void listButton_Click(object sender, EventArgs e)
{
DataTable dtServer = SqlDataSourceEnumerator.Instance.GetDataSources();
serverDetailsGrid.DataSource = dtServer;
}
}
}

you will get the following result in the grid

List of servers on network

List of servers on network

Written by A.Sethi

September 10, 2008 at 2:23 pm