Pages

Showing posts with label Code Snips. Show all posts
Showing posts with label Code Snips. Show all posts

Friday, July 4, 2008

xp_cmdshell

Executes a given command string as an operating-system command shell and returns any output as rows of text. Grants nonadministrative users permissions to execute xp_cmdshell.

Executing this xp_cmdshell statement returns a directory listing of the current directory.


xp_cmdshell 'dir *.exe'

xp_cmdshell operates synchronously. Control is not returned until the command shell command completes.

When you grant execute permissions to users, the users can execute any operating-system command at the Microsoft Windows NT command shell that the account running Microsoft SQL Server™ has the needed privileges to execute.

By default, only members of the sysadmin fixed server role can execute this extended stored procedure. You may, however, grant other users permission to execute this stored procedure.

Thursday, June 26, 2008

Override ToString method

This C# code snippet overrides the ToString method inherited from System.Object in order to display the private variables.

public class MyClass
{
private string customer ="";
private int customerID = 0;

public string Customer
{
get { return customer; }
set { customer = value; }
}

public int CustomerID
{
get { return customerID; }
set { customerID = value; }
}

public override string ToString()
{
return string.Format ("Customer = {0} ID = {1}", Customer, CustomerID);
}

}

Tuesday, June 24, 2008

Sort Array

This C# code snippet sorts the elements of an array into ascending order


public class ReverseArraySort
{

public static void Main()
{
string[] strings = {"beta", "alpha", "gamma"};
Console.WriteLine ("Array elements: ");
DisplayArray (strings);
Array.Sort (strings); // Sort elements
DisplayArray (strings);
}

public static void DisplayArray (Array array)
{
foreach (object o in array)
{
Console.Write ("{0} ", o);
}
Console.WriteLine();
}
}

Thursday, June 19, 2008

Retrieve Current Screen Resolution

Gets the dimensions, in pixels, of the current video mode of the primary display

MessageBox.Show( "Monitor Size:" + SystemInformation.PrimaryMonitorSize );

Thursday, June 12, 2008

Call with variable arguments

Sometimes we may require a variable number of arguments to be passed to a function. For example we may require a sum function which calculates the total of the numbers passed to it no matter how many numbers are passed.

In C# we can use the Params keyword and pass variable no of arguments to a function. It's much like using ParamArray in VisualBasic language. The syntax of params arguments is:
params datatype[] argument name

static void ListArguments (params object[] arguments)
{
foreach (object argument in arguments)
{
Console.WriteLine (argument);
}
}

public static void Main( )
{
ListArguments ("Arguments: ", DateTime.Now, 3.14f);
}

Saturday, June 7, 2008

Calculate Fibonacci number

static int Fibonacci (int x)
{
Console.WriteLine ("x = {0}", x);
if (x <= 1)
{
return 1;
}
return Fibonacci (x-1) + Fibonacci (x-2);
}

static void Main( )
{
Console.WriteLine ("Fibonacci no. = {0}", Fibonacci (5));
Console.ReadKey();
}

Monday, June 2, 2008

Write a Registry key

using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey
("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
{
try
{
masterKey.SetValue ("MyKey", "MyValue");
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
finally
{
masterKey.Close();
}
}

Read a Registry key

using Microsoft.Win32;
...
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey
("SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
Console.WriteLine ("Null Masterkey!");
}
else
{
Console.WriteLine ("MyKey = {0}", masterKey.GetValue ("MyKey"));
}
masterKey.Close();