Tuesday, December 30, 2008

Hashtable vs Dictionary<>

Hashtable, array, and ArrayList ---- System.Collections, weak data type
Dictionary<>, List<> -----System.Collection.Generic, strong data type

for example:
you can define hashtable:


Hashtable employees = new Hashtable();
employees.Add("A100",1);
employees.Add("A101",2);
employees.Add(3, "asdfasf");

no compile error

Dictinary diEmp = new Dictionary();

Reading List

Design Guidelines for developing class libraries:

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

Monday, December 29, 2008

Using sql agent date and time handling

I had project try to insert batch run time into database. The batchRunDt data formation is "20070430204147", the field in database is datetime,

here is the example to show you how to using fn_agentDate2DateTime to converts datetime


declare @batchRunDt varchar(20)
select @batchRunDt = '20070430204147'
print msdb.dbo.fn_AgentDateTime2DateTime(left(@batchRunDt,8),Right(@batchRunDt,6))

The result is :
Apr 30 2007 8:41PM

print msdb.dbo.fn_AgentDate2DateTime(20060806)
The result is:

Aug 9 2006 12:00AM



you can download the function in : http://sqldev.net/sqlagent/SQLAgentDateTime.htm

Find the missing two interger in array

We all know how to find one missing interger in array, my question is how can we find the two missing interger in interger array:

public void Find2MissingNum()
{
int[] iTol = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] iMis = { 1, 2, 4, 6, 7, 8, 9, 10 };
int x, y;
// try to find 3, 5
//sum iMis
int sMis =0 , sTol=0;
foreach (int i in iMis)
{
sMis += i;
}
// sum iTol
sTol = iTol.Length * (iTol.Length + 1) / 2;
// time total
int tMis = 1, tTol = 1;
foreach (int i in iMis)
{
tMis *= i;
}
foreach (int i in iTol)
{
tTol *= i;
}
int timeSmisStol = tTol / tMis;
int difTM = sTol - sMis;
for (int i = 0; i < difTM; i++)
{
if (i * (difTM - i) == timeSmisStol)
{
x = i;
y = difTM - i;
}
}



}

Tuesday, December 23, 2008

REST Web Services Characteristics

Client- Server: a pull based interaction style: consuming components pull representtaions.
Stateless: each request from client to server smust contain all the information necessary to understand the request, and cannot take advantage of any stored contect on the server.
Cache:to impreove network efficiency responses must be capable of being labeled as cacheable or non-cacheable.
Uniform interface: all resources are accessed with a generic interface(http get, post, put, delete)
Named resources -- the system is comprised of resources which are names using a URL.
interconnected resource representations -- the representations of the resources are interconnected using URLs, thereby enabling a client to progress from one state to another.
Layered componens-- intermediaries, such as proxy servers, cache servers, gateways, etc, can be inserted between clients and resources to support performance, security, etc.

REST-- An architectural style, not a standard

REST is not a standard, it does use standards:

HPPT,
URL,
XML/HTML/GIF/JPEG/etc(Resource Representations),
text/xml,text/html/image/gif,
image/jpeg, etc

REST--- Representational State Transfer

REST Data Elements:
resource --- the intended conceptual target of a hypertext reference
resource identifier --- url, urn
REPRESENTATION ---html document, JPEG image
representation metadata -- media tyoe, last- modified time
resource metadata -- source link, alternates, vary
control data -- if modified- since, cache- control

Wednesday, December 17, 2008

Function to find second highest integer

private int FindSecondHighestInt()
{
int[] testArray = { 6,4, 7, 9, 10, 3, 2 };
int highest, secondHighest, temp;
highest = testArray[0];
secondHighest = testArray[0];
for (int i = 0; i < testArray.Length; i++)
{
temp = testArray[i];
if ( temp > highest)
{
secondHighest = highest;
highest = temp;
}
else
if (temp > secondHighest)
{
secondHighest = temp;
}
}
return secondHighest;
}
_____________________________________________

Wednesday, December 3, 2008

Compare XML data with SQL 2000 table

In SQL 2000, I wrote some code in stored procedure in SQL 2000 openxml. I need check for the data duplication before inserting the value,
Create Proc prdSaveHPCatalog(@xml TEXT, @brand varchar(200), @isExisted bit output)
AS
SET NOCOUNT ON

DECLARE @IDocumentHandle int
DECLARE @isExisted bit

EXECUTE sp_xml_preparedocument @iDocumentHandle output, @xml
---- check if the data already existed
if not exists( select 1 from OpenXML(@iDocumnetHandle, '/productCatalog/header',2) WITH (batchRunDt varchar(20) 'barchRunDt') HeaderRunTime WHERE HeaderRunTime.batchRunDt in (select batchRunDt from CategoryHeader)
)
set @isExisted = 1

Tuesday, December 2, 2008

Improving the Search Time with Binary Search Trees

The important concept to understand the BST is that ideally at each step in the algorithm that number of nodes that have to be considered has been cut in half. With an ideally arrangd BST the midpoint is the root. We than traverse down the tree, navigating to the left and right children as needed. These approaches cut the search space in half at each step. Such algorithms that exhibit this property have an asymptotic running time of log2n,