Jun 26, 2007

SQL Server Projects in C# - Some important links








  1. How to: Connect to a Database -
    http://msdn2.microsoft.com/en-us/library/ms165037(VS.80).aspx
  2. CLR Integration Security Issues -
    http://msdn2.microsoft.com/en-us/library/ms254940(VS.80).aspx
  3. Advantages of Using Managed Code to Create Database Objects -
    http://msdn2.microsoft.com/en-us/library/k2e1fb36(VS.80).aspx
  4. Item Templates for SQL Server Projects -
    http://msdn2.microsoft.com/en-us/library/eaf9k1as(VS.80).aspx
  5. Accessing Application Configuration Settings from SQL CLR -
    http://sqljunkies.com/Article/6CA864E9-E107-408E-B30A-4BA15B0CD11C.scuk
  6. Introducing SQL Server 2005's CLR Integration -
    http://www.devsource.com/print_article2/0,1217,a=151093,00.asp
  7. CLR Integration Programming Model Restrictions -
    http://msdn2.microsoft.com/en-us/library/ms403273.aspx
  8. Invoke WCF Service via SQL -
    http://blogs.msdn.com/saradhic/archive/2007/09/12/invoke-wcf-service-via-sql.aspx
  9. SQL CLR and WCF Compatibility -
    http://blogs.neudesic.com/blogs/shaun_collett/archive/2007/04/29/6048.aspx




  1. SQL Server Projects -
    http://msdn2.microsoft.com/en-us/library/c8dbfz8s(VS.80).aspx
  2. Introduction to SQL Server CLR Integration -
    http://msdn2.microsoft.com/en-us/library/ms254498(VS.80).aspx
  3. Enabling CLR Integration -
    http://msdn2.microsoft.com/en-us/library/ms254506(VS.80).aspx
  4. Compiling and Deploying a CLR Assembly -
    http://msdn2.microsoft.com/en-us/library/ms254956(VS.80).aspx
  5. How to: Debug a SQL CLR Stored Procedure -
    http://msdn2.microsoft.com/en-us/library/ms165051(VS.80).aspx
  6. Debugging SQL Database Objects -
    http://msdn2.microsoft.com/en-us/library/ms165034(VS.80).aspx
  7. Setting Up SQL Debugging -
    http://msdn2.microsoft.com/en-us/library/s4sszxst(VS.80).aspx


Jun 20, 2007

C# : Drawing a Pie Chart and saving as jpeg

////Namespace:
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;

////Create canvas and fill the background
Bitmap objBitmap;
Graphics objGraphics;

objBitmap = new Bitmap(400, 440);
objGraphics = Graphics.FromImage(objBitmap);


objGraphics.Clear(Color.White);

////Draw the pie and fill
Pen p=new Pen(Color.Yellow,0);
Rectangle rect=new Rectangle(10,10,280,280);
objGraphics.DrawEllipse(p,rect);

Brush b1=new SolidBrush(Color.Red);
Brush b2=new SolidBrush(Color.Green);
Brush b3=new SolidBrush(Color.Blue);
objGraphics.FillPie(b1, rect, 0f, 60f);
objGraphics.FillPie(b2, rect, 60f, 150f);
objGraphics.FillPie(b3, rect, 210f, 150f);

////Draw font
FontFamily fontfml = new FontFamily(GenericFontFamilies.Serif);
Font font = new Font(fontfml, 16);
SolidBrush brush = new SolidBrush(Color.Blue);
objGraphics.DrawString("Drawing Graphics", font, brush, 70, 300);

////Export and save to picture
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
objBitmap.Save(Server.MapPath("x.jpg"), ImageFormat.Jpeg);

////End drawing
objBitmap.Dispose();
objGraphics.Dispose();

TableLayoutPanel in C#

The Tablelayoutpanel is a easy way to add Controls in a Tabular format.

////--------Sample code-------
TextBox questionText = new TextBox();
questionText.Text = questionName;
questionText.MouseHover += new EventHandler(Question_MouseHover);
questionText.Width = 200;
questionText.BorderStyle = BorderStyle.FixedSingle;
questionText.BackColor = Color.Black;
questionText.ForeColor = Color.White;

GroupBox grRadios = new GroupBox();
grRadios.Height = 25;
grRadios.Width = 100;
grRadios.Padding = new Padding(2);
grRadios.Tag = questionId.ToString();
grRadios.Text = string.Empty;

RadioButton[] radios = new RadioButton[3];
radios[0] = new RadioButton();
radios[0].Tag = "Yes";
radios[1] = new RadioButton();
radios[1].Tag = "No";
radios[2] = new RadioButton();
radios[2].Tag = "Not Applicable";

////Add a event handler to the radio buttons
for (int i = 0; i < radios.Length; i++)
{
radios[i].Width = 20;
radios[i].MouseHover += new EventHandler(Radio_MouseHover);
if (i == checkedRadio)
{
radios[i].Checked = true;
}
}

grRadios.Controls.AddRange(radios);

radios[0].Left = 0;
radios[0].Top = radios[0].Top;
radios[1].Left = radios[1].Left + 20;
radios[1].Top = radios[0].Top;
radios[2].Left = radios[2].Left + 20;
radios[2].Top = radios[0].Top;


tblQuestions.Controls.Add(questionText);
tblQuestions.Controls.Add(grRadios);

SQL Server - Parse a delimited string


alter procedure web_ParseArray
( @Array varchar(1000),
@separator char(1) )
AS
-- Created by graz@sqlteam.com
-- Modified to result a table by Bart@prove.be

set nocount on
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
declare @separator_position int -- This is used to locate each separator character
declare @array_value varchar(1000) -- this holds each array value as it is returned

create table #ParsedArrays (array_Value varchar(1000))

-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
set @array = @array + @separator

-- Loop through the string searching for separtor characters
while patindex('%' + @separator + '%' , @array) <> 0
begin

-- patindex matches the a pattern against a string
select @separator_position = patindex('%' + @separator + '%' , @array)
select @array_value = left(@array, @separator_position - 1)

-- This is where you process the values passed.
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array
insert #ParsedArrays VALUES (@array_value)


-- This replaces what we just processed with and empty string
select @array = stuff(@array, 1, @separator_position, '')
end

set nocount off
select * from #ParsedArrays
drop table #ParsedArrays
go