Friday, December 11, 2009

C# Tutorial - Using The ThreadPool

http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool

VSTO Drag & Drop - Application_SheetChange event handler

http://msdn.microsoft.com/en-us/library/bb840032.aspx

SQLCLR : Table Valued Function


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

class Product
{
public int Quantity { get; set; }
public string Description { get; set; }
}


public partial class UserDefinedFunctions
{

[SqlFunction(TableDefinition = @"Quantity int,
Description nvarchar(100)",
Name = "Products",
FillRowMethodName = "FillRowProduct")]
public static IEnumerable GetProducts()
{

List lstPerson = new List();
lstPerson.Add(new Product { Quantity = 1, Description = "Ball" });
lstPerson.Add(new Product { Quantity = 10, Description = "Bike" });
lstPerson.Add(new Product { Quantity = 5, Description = "Club" });
lstPerson.Add(new Product { Quantity = 10, Description = "Bat" });
return lstPerson;
}


public static void FillRowProduct(Object obj, out SqlInt32 Quantity, out SqlChars Description)
{
Product p = (Product)obj;
Description = new SqlChars(p.Description);
Quantity = new SqlInt32(p.Quantity);
}
};

<--->
SELECT Prod.*
FROM dbo.Products() AS Prod

Quantity Description
1    Ball
10    Bike
5    Club
10    Bat

Sunday, December 6, 2009

CLR Stored Procedures

http://www.sqldbatips.com/showarticle.asp?ID=22
http://www.aspfree.com/c/a/MS-SQL-Server/NET-CLR-stored-procedures-in-SQL-Server-2005-DB-Good-News-to-Programmers-Shock-to-DBAs/2/

C# Code Snippets

http://en.csharp-online.net/CSharp_Code_Snippets

VSTO Excel From Range to Array Array to Range


using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelArray
{
public partial class ThisAddIn
{
Excel.Worksheet ws;
Excel.Range c;
Object[,] arValue;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;

for (int j = 1; j < 11; j++)
{
for (int i = 1; i < 11; i++)
{
c = (Excel.Range)ws.Cells[i, j];
c.Value2 = i+j-1;
}
}

// From Range to Array
// We copy the 2nth column
c = (Excel.Range)ws.get_Range(ws.Cells[1, 2], ws.Cells[10, 2]);
c.Interior.ColorIndex = 2;
arValue = (Object[,])c.Value2;

// Array to Range
// We paste on the first column
c = (Excel.Range)ws.get_Range(ws.Cells[12, 1], ws.Cells[21, 1]);
c.Value2 = arValue;
c.Interior.ColorIndex = 6;
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}

#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}