Saturday, June 14, 2014

How to get list of available SQL Servers instances using C#

/// <summary>
/// Get SQL Server Instances if possible
/// </summary>
private static string[] GetSQLServerInstances()
{
    List<string> instances = new List<string>();
    try
    {
        System.Data.Sql.SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
        System.Data.DataTable table = instance.GetDataSources();
 
        foreach (System.Data.DataRow row in table.Rows)
        {
            instances.Add(row[0] + "\\" + row[1]);
        }
    }
    catch (Exception)
    {
 
    }
    return instances.ToArray();
}

Deserialization error in XML document(1,1)

Use StreamReader instead of StringReader

Generate C# class from XML

Using XSD

from visual studio tools menu run "Developer Command Prompt for VS2013" as administrator

switch to the directory where your xml file exists using "cd yourxmlfolder"
then type

d:\yourxmlfolder> xsd yourxmlfile.xml

this will generate a yourxmlfile.xsd

then type

d:\yourxmlfolder> xsd yourxmlfile.xsd /classes

and your done

Tuesday, June 10, 2014

How to get the 4(n) right / left most numbers from an integer SQl SERVER

n right most numbers from an integer
SELECT CAST(RIGHT(CAST(Int AS VARCHAR(20)), [n]) AS BIGINT)
n left most numbers from an integer
SELECT CAST(LEFT(CAST(Int AS VARCHAR(20)), [n]) AS BIGINT)

Tuesday, June 3, 2014

Microsoft HTTPAPI/2.0 use Port 80 – Cannot Start WAMP Apache

Stop the SQL Server Reporting Services

How to check if a byte array is a valid image?

public static bool IsValidImage(byte[] bytes)
{
    try
    {
        using (MemoryStream ms = new MemoryStream(bytes))
            System.Drawing.Image.FromStream(ms);
    }
    catch (ArgumentException)
    {
        return false;
    }
    return true;
}

Convert int to guid in C# and SQL Server

C#
public static Guid Int2Guid(int value)

{
    byte[] bytes = new byte[16];
    BitConverter.GetBytes(value).CopyTo(bytes, 0);
    return new Guid(bytes);
}
SQL Server

CAST(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), 13))) AS uniqueidentifier)

How to change screen orientation in android emulator?

Ctrl+F12  or Num 7 is the keyboard shortcut.