Wednesday, May 11, 2011

Generic with Lamda Expression

public void Each(IEnumerable items, Action action)
{
foreach (var item in items)
{
action(item);
}
}

public void Main()
{
List myList = new List();
myList.Add(1);
myList.Add(3);

Each(myList, i => textBox1.AppendText(i.ToString()+ "\r\n") );

new List(myList).ForEach(i => textBox1.AppendText(i.ToString() + "\r\n"));

}

Timeout Function for C#.Net

public static T Limex(Func F, int Timeout, out bool Completed)
{
var iar = F.BeginInvoke(null, new object());
if (iar.AsyncWaitHandle.WaitOne(Timeout))
{
Completed = true;
return F.EndInvoke(iar);
}
Completed = false;
return default(T);
}

// Overloaded method, for cases when we don't
// need to know if the method was terminated
public static T Limex(Func F, int Timeout)
{
bool Completed;
return Limex(F, Timeout, out Completed);
}

private string Wait()
{
Thread.Sleep(11000);
return "Paresh";
}

private bool Wait(string name)
{
return false;
}

public void Main()
{
bool Completed = false;
string str = Limex(() => Wait() , 10000, out Completed);
Completed = false;
bool b = Limex(() => Wait("paresh"), 10000, out Completed);
}

Thursday, September 24, 2009

Query to select records from Xml

I have following Xml in Job table which column name is JobXml (Xml datatype)

<Job>
<TimeID Value="2323" id="a"/>
<TimeID Value="4445" id="b"/>
<TimeID Value="3453" id="a"/>
<TimeID Value="7677" id="c"/>
</Job>

I want to write a select query who returns all TimeID Value where id= "a" like this

2323
3453

Following is the query for that:

SELECT r.value('@Value','int')
FROM Job
CROSS APPLY StatisticsXml.nodes('/Job/TimeID[@id=''a'']') AS x(r)

Wednesday, September 23, 2009

Convert ArrayList into Array

ArrayList arrayList = new ArrayList();
arrayList.Add("paresh");
arrayList.Add("sonal");
string[] stringArray = (string[])arrayList.ToArray(typeof(string));

Read Xml Value in SQL Server 2005

Hi,

Here I am giving the example how to read XML fields in SQL Server query.

Database Table jobtable contains jobxml field which datatype is xml
You can query to read xml. It is very simple to use.


Suppose you have following XML :

<scheduler_job>
<submitter>
<email notify="false" id="5">pdehadray</email>
</submitter>
</scheduler_job>


Query to read email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[1]','varchar(50)') from jobtable

Query to read notify attribute value in email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[1]','varchar(50)') from jobtable

Query to read id attribute value in email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[1]','int') from jobtable



If XML contains two node with same name like this:

<scheduler_job>
<submitter>
<email notify="false" id="5">pdehadray</email>
<email notify="true" id="6">rjaiswal</email>
</submitter>
</scheduler_job>


Query to read 1st email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[1]','varchar(50)') from jobtable

Query to read 2nd email node inner text is as follows:
select jobxml.value('(/scheduler_job/submitter/email)[2]','varchar(50)') from jobtable



Query to read notify attribute value in 1st email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[1]','varchar(50)') from jobtable

Query to read notify attribute value in 2nd email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@notify)[2]','varchar(50)') from jobtable



Query to read id attribute value in 1st email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[1]','int') from jobtable

Query to read id attribute value in 2nd email node is as follows:
select jobxml.value('(/scheduler_job/submitter/email/@id)[2]','int') from jobtable

Hope it will be useful for you.