Tuesday, March 29, 2011

Using Table-Valued Function in MS SQL

Code:
-- SET NOCOUNT ON 

CREATE FUNCTION DatesBetween2()
RETURNS @dates TABLE (
DateValue date NOT NULL PRIMARY KEY CLUSTERED
)
as
begin
DECLARE @startDate DATETIME = GETDATE();
DECLARE @endDate DATETIME = DATEADD(day, 9999, GETDATE());
BEGIN
WHILE (@startDate <= @endDate) BEGIN
INSERT INTO @dates VALUES (@startDate);
SET @startDate = DATEADD(day, 1, @startDate);
END;
RETURN;
END;
END;

select * from dbo.DatesBetween2()

Thursday, March 17, 2011

What is message queuing?

Message queuing has been used in data processing for many years. It is most commonly used today in electronic mail. Without queuing, sending an electronic message over long distances requires every node on the route to be available for forwarding messages, and the addressees to be logged on and conscious of the fact that you are trying to send them a message. In a queuing system, messages are stored at intermediate nodes until the system is ready to forward them. At their final destination they are stored in an electronic mailbox until the addressee is ready to read them.

Even so, many complex business transactions are processed today without queuing. In a large network, the system might be maintaining many thousands of connections in a ready-to-use state. If one part of the system suffers a problem, many parts of the system become unusable.

You can think of message queuing as being electronic mail for programs. In a message queuing environment, each program from the set that makes up an application suite is designed to perform a well-defined, self-contained function in response to a specific request. To communicate with another program, a program must put a message on a predefined queue. The other program retrieves the message from the queue, and processes the requests and information contained in the message. So message queuing is a style of program-to-program communication.
Queuing is the mechanism by which messages are held until an application is ready to process them. Queuing allows you to:

* Communicate between programs (which might each be running in different environments) without having to write the communication code.
* Select the order in which a program processes messages.
* Balance loads on a system by arranging for more than one program to service a queue when the number of messages exceeds a threshold.
* Increase the availability of your applications by arranging for an alternative system to service the queues if your primary system is unavailable.

How to: Access Binary Data as a Stream (WCF Data Services/Silverlight)

Silverlight

Open Data Protocol (OData) enables a data service to make binary data available outside of the feed itself. This binary data, called a media resource, is separate from but related to an entity, which is called a media link entry. For more information, see Working with Binary Data (WCF Data Services).

The procedure and examples in this topic show you how to add a reference to the Northwind streaming sample data service and call the GetReadStream(Object) method to retrieve binary data as a stream from an OData service.

The streaming data service accessed by the application is a special version of the Northwind sample data service that has been modified to support streaming. For more information see Streaming Provider (WCF Data Services). The Northwind streaming data service sample can be downloaded from the MSDN Code Gallery Web site.

Check the following link:
http://msdn.microsoft.com/en-us/library/ff602281%28v=VS.95%29.aspx

Tuesday, March 8, 2011

Generates Random Numbers

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

public class MersenneTwister
{
// Class MersenneTwister generates random numbers
// from a uniform distribution using the Mersenne
// Twister algorithm.
private const int N = 624;
private const int M = 397;
private const uint MATRIX_A = 0x9908b0dfU;
private const uint UPPER_MASK = 0x80000000U;
private const uint LOWER_MASK = 0x7fffffffU;
private const int MAX_RAND_INT = 0x7fffffff;
private uint[] mag01 = { 0x0U, MATRIX_A };
private uint[] mt = new uint[N];
private int mti = N + 1;

public MersenneTwister()
{ init_genrand((uint)DateTime.Now.Millisecond); }
public MersenneTwister(int seed)
{
init_genrand((uint)seed);
}
public MersenneTwister(int[] init)
{
uint[] initArray = new uint[init.Length];
for (int i = 0; i < init.Length; ++i)
initArray[i] = (uint)init[i];
init_by_array(initArray, (uint)initArray.Length);
}

public static int MaxRandomInt
{ get { return 0x7fffffff; } }

public int Next()
{ return genrand_int31(); }
public int Next(int maxValue)
{ return Next(0, maxValue); }
public int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
int tmp = maxValue;
maxValue = minValue;
minValue = tmp;
}
return (int)(Math.Floor((maxValue - minValue + 1) * genrand_real1() +
minValue));
}

public float NextFloat()
{ return (float)genrand_real2(); }
public float NextFloat(bool includeOne)
{
if (includeOne)
{
return (float)genrand_real1();
}
return (float)genrand_real2();
}
public float NextFloatPositive()
{ return (float)genrand_real3(); }
public double NextDouble()
{ return genrand_real2(); }
public double NextDouble(bool includeOne)
{
if (includeOne)
{
return genrand_real1();
}
return genrand_real2();
}

public double NextDoublePositive()
{ return genrand_real3(); }
public double Next53BitRes()
{ return genrand_res53(); }
public void Initialize()
{ init_genrand((uint)DateTime.Now.Millisecond); }
public void Initialize(int seed)
{ init_genrand((uint)seed); }
public void Initialize(int[] init)
{
uint[] initArray = new uint[init.Length];
for (int i = 0; i < init.Length; ++i)
initArray[i] = (uint)init[i];
init_by_array(initArray, (uint)initArray.Length);
}
private void init_genrand(uint s)
{
mt[0] = s & 0xffffffffU;
for (mti = 1; mti < N; mti++)
{
mt[mti] =
(uint)(1812433253U * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
mt[mti] &= 0xffffffffU;
}
}
private void init_by_array(uint[] init_key, uint key_length)
{
int i, j, k;
init_genrand(19650218U);
i = 1; j = 0;
k = (int)(N > key_length ? N : key_length);
for (; k > 0; k--)
{
mt[i] = (uint)((uint)(mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525U
)) + init_key[j] + j);
mt[i] &= 0xffffffffU;
i++; j++;
if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
if (j >= key_length) j = 0;
}
for (k = N - 1; k > 0; k--)
{
mt[i] = (uint)((uint)(mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) *
1566083941U)) - i);
mt[i] &= 0xffffffffU;
i++;
if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
}
mt[0] = 0x80000000U;
}

uint genrand_int32()
{
uint y;
if (mti >= N)
{
int kk;
if (mti == N + 1)
init_genrand(5489U);
for (kk = 0; kk < N - M; kk++)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
}
for (; kk < N - 1; kk++)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U];
}
y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];
mti = 0;
}
y = mt[mti++];
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680U;
y ^= (y << 15) & 0xefc60000U;
y ^= (y >> 18);
return y;
}
private int genrand_int31()
{ return (int)(genrand_int32() >> 1); }
double genrand_real1()
{ return genrand_int32() * (1.0 / 4294967295.0); }
double genrand_real2()
{ return genrand_int32() * (1.0 / 4294967296.0); }
double genrand_real3()
{ return (((double)genrand_int32()) + 0.5) * (1.0 / 4294967296.0); }
double genrand_res53()
{
uint a = genrand_int32() >> 5, b = genrand_int32() >> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
}
}
public class Program
{
static void Main()
{
MersenneTwister randGen = new MersenneTwister();
Console.WriteLine("100 uniform random integers in [0,{0}]:", MersenneTwister.MaxRandomInt);
int i;

for (i = 0; i < 100; ++i)
{
Console.Write("{0} ", randGen.Next());
if (i % 5 == 4) Console.WriteLine("");
}
Console.WriteLine("100 uniform random doubles in [0,1]:");
for (i = 0; i < 100; ++i)
{
Console.Write("{0} ", randGen.NextDouble().ToString("F8"));
if (i % 5 == 4) Console.WriteLine("");
}
Console.WriteLine("Press ENTER to quit");
Console.ReadLine();
}
}
}

Thursday, March 3, 2011

C# COPY AND RENAME

Copy file from one folder to another folder, add different prefix to all file.

Code:

using System;
using System.IO;


namespace IOProject
{
class Class1
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Please enter the name of the folder you want to create or use");
// store the folder name in the dirName variable
//string dirName = Console.ReadLine();
string dirName = @"C:\stephen wu\tools\audioBible";
// create the DirectoryInfo instance
//DirectoryInfo dInfo = new DirectoryInfo(@"F:" + dirName);
DirectoryInfo dInfo = new DirectoryInfo(dirName);
string targetDirectory = @"C:\stephen wu\tools\audio2";
DirectoryInfo[] subDirs = dInfo.GetDirectories();
if (subDirs.Length > 0)
{
Console.WriteLine("Directories:");
foreach (DirectoryInfo subDir in subDirs)
{
Console.WriteLine(" " + subDir.Name);

int atmp = subDir.Name.IndexOf("a");
string l4string = subDir.Name.Substring(0, atmp+1);
//l4string = l4string + "_";

Console.WriteLine(" " + l4string);

string sourceDirectory = @"C:\stephen wu\tools\audioBible\" + subDir.Name;
Rename(sourceDirectory,targetDirectory, l4string);
Console.WriteLine(" " + sourceDirectory);
}
}

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadLine();
}
}

public static void Rename(string sourceDirectory, string targetDirectory, string Prefixtmp)
{
DirectoryInfo di = new DirectoryInfo(sourceDirectory);
if (di != null)
{

FileInfo[] subFiles = di.GetFiles();
if (subFiles.Length > 0)
{
Console.WriteLine("Files:");
foreach (FileInfo subFile in subFiles)
{
Console.WriteLine(" " + subFile.Name + " (" + subFile.Length + " bytes)");

string newname = targetDirectory + "\\" + Prefixtmp + subFile.Name.ToString();
string oldname =sourceDirectory+"\\"+subFile.Name.ToString();
File.Copy(oldname, newname);
}
}
//Console.ReadKey();
}
}
}
}