Search This Blog

Thursday, March 26, 2009

Convert classes to XML and back

From http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm

// This is the test class we want to
// serialize:
[Serializable()]
public class TestClass
{
private string someString;
public string SomeString
{
get { return someString; }
set { someString = value; }
}

private List settings = new List();
public List Settings
{
get { return settings; }
set { settings = value; }
}

// These will be ignored
[NonSerialized()]
private int willBeIgnored1 = 1;
private int willBeIgnored2 = 1;

}

// Example code

// This example requires:
// using System.Xml.Serialization;
// using System.IO;

// Create a new instance of the test class
TestClass TestObj = new TestClass();

// Set some dummy values
TestObj.SomeString = "foo";

TestObj.Settings.Add("A");
TestObj.Settings.Add("B");
TestObj.Settings.Add("C");


#region Save the object

// Create a new XmlSerializer instance with the type of the test class
XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));

// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);

// Cleanup
WriteFileStream.Close();

#endregion


/*
The test.xml file will look like this:



foo

A
B
C


*/

#region Load the object

// Create a new file stream for reading the XML file
FileStream ReadFileStream = new FileStream(@"C:\test.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

// Load the object saved above by using the Deserialize function
TestClass LoadedObj = (TestClass)SerializerObj.Deserialize(ReadFileStream);

// Cleanup
ReadFileStream.Close();

#endregion


// Test the new loaded object:
MessageBox.Show(LoadedObj.SomeString);

foreach (string Setting in LoadedObj.Settings)
MessageBox.Show(Setting);

locking a table on insert statement

INSERT INTO [table name] WITH (TABLOCK)
()
SELECT FROM [table name] WHERE

Show locks on database server

run sp_lock

Get Database ID and name

SELECT DB_ID() DatabaseID, DB_NAME() DatabaseNM

Thursday, March 19, 2009

A Fine Frenzy - Rangers

The song writer obviously liked the move Watership down...


Lyrics | A Fine Frenzy Lyrics | Rangers Lyrics

Amy MacDonald - This is the life



Lyrics | Amy McDonald Lyrics | This Is The Life Lyrics

Monday, March 16, 2009

Formatting Dates

Because MS online help is often too slow...

Without century (yy) (1) With century (yyyy) Standard Input/Output (3)

-

0 or 100 (1, 2)

Default

mon dd yyyy hh:miAM (or PM)

1

101

U.S.

mm/dd/yyyy

2

102

ANSI

yy.mm.dd

3

103

British/French

dd/mm/yyyy

4

104

German

dd.mm.yy

5

105

Italian

dd-mm-yy

6

106 (1)

-

dd mon yy

7

107 (1)

-

Mon dd, yy

8

108

-

hh:mi:ss

-

9 or 109 (1, 2)

Default + milliseconds

mon dd yyyy hh:mi:ss:mmmAM (or PM)

10

110

USA

mm-dd-yy

11

111

JAPAN

yy/mm/dd

12

112

ISO

yymmdd

yyyymmdd

-

13 or 113 (1, 2)

Europe default + milliseconds

dd mon yyyy hh:mi:ss:mmm(24h)

14

114

-

hh:mi:ss:mmm(24h)

-

20 or 120 (2)

ODBC canonical

yyyy-mm-dd hh:mi:ss(24h)

-

21 or 121 (2)

ODBC canonical (with milliseconds)

yyyy-mm-dd hh:mi:ss.mmm(24h)

-

126 (4)

ISO8601

yyyy-mm-ddThh:mi:ss.mmm (no spaces)

-

127(6, 7)

ISO8601 with time zone Z.

yyyy-mm-ddThh:mi:ss.mmmZ

(no spaces)

-

130 (1, 2)

Hijri (5)

dd mon yyyy hh:mi:ss:mmmAM

-

131 (2)

Hijri (5)

dd/mm/yy hh:mi:ss:mmmAM

1 These style values return nondeterministic results. Includes all (yy) (without century) styles and a subset of (yyyy) (with century) styles.

2 The default values (style 0 or 100, 9 or 109, 13 or 113, 20 or 120, and 21 or 121) always return the century (yyyy).

3 Input when you convert to datetime; output when you convert to character data.

4 Designed for XML use. For conversion from datetime or smalldatetime to character data, the output format is as described in the previous table.

5 Hijri is a calendar system with several variations. SQL Server uses the Kuwaiti algorithm.

Saturday, March 14, 2009

Tricks to extend primary Disk partiton

Just learned this trick from a friend at work.
Still learning Vista. Not sure why people do not like it. There is alot to like...
How To Mount and Access New Partition, Volume or Drive As Folder Path In Windows




When adding a new hard disk or create a new partition in Microsoft Windows operating system
, typically users will normally assign a drive letter such as Local Disk (E:) to the new partition or volume. Beside normal way of allocating a drive letter, it’s also possible to assign a folder or directory (on partitions formated as NTFS) to represent the hard disk drive, partition or volume too, eliminating eliminating the use of drive letter and enabling browsing the volume and partition contents as in folder, or path.Mounting a partition or drive as a folder also provides a workaround way or trick to allow a partition to appear as part of C:\ root system drive. This hack is particular useful to expand the hard disk space size of C:\ or other system drive without having to use a new disk drive letter or switching drive to accommodate older program or application that must store and save data on installed drive, typically at system drive.
To mount partition or volume as a folder, users must first have or create a empty folder on a NTFS formatted file system that users want to assign or associate with the partition or volume. And it’s also possible to mount a partition, a volume or a hard disk drive which already assigned with a disk drive letter as folder too, allowing two ways to access the volume or partition via different drive or different folder. And it’s not necessary that only a hard disk (which whole hard disk been allocated as single partition) can be mounted as an empty NTFS folder, unused partition or empty space on existing disk can be mounted as NTFS folder too as long as users can create a new partition on it.
How to Mount a New Volume/Partition as Folder
Click on Start Menu, then select Run. In Vista, press Win+R key to open Run command.
Type DiskMgmt.msc to open Disk Management.
Right click on the new hard disk or remaining unallocated space, and then select New Partition or New Simple Volume (in Vista). If the partition has been created, delete the partition and re-create it.
For partition that has been allocated with drive letter and path, right click on the partition and select Change Drive Letter and Paths.
In the “New Simple Volume Wizard” or “New Partition Wizard”, follow the instructions on screen such as selecting the size of the new partition, until the the Assign Drive Letter or Path screen.
For existing volume/partition, click on Add button to add a new path instead.
Select Mount in the following empty NTFS folder radio button, and then select Browse… button.
For existing volume/partition, the “Mount in the following empty NTFS folder” is automatically selected. Users cannot assign more than one drive letter to a partition, but can assign many paths (folders) to a partition.
Select an empty folder that you want to mount this new partition. Click on New Folder to create a new directory if needed. Once selected the mounting folder, click OK button.
For existing partition already with drive letter, process is completed and users can now open Windows Explorer to browse the partition content in the mounted folder.
Else, back in the wizard, continue to follow instructions on screen to complete the wizard by selecting file system, allocation unit size, volume label, decide whether to perform quick format or whether to enable file and folder compression. Wait for the disk formatting to complete too. After process completed, a folder is linked to the partition/volume.
As mentioned, users can assigned many folder path to a single volume or partition. Users can also remove the relationship of folder mount point with volume at any time, or change back to a drive letter. Even already mounted on a folder path, a drive letter can still be assigned to the partition too. All these administrative operations can be done on the Disk Management. Best of all, changing drive letter or folder paths of a volume will not delete the data on the drive.
window.google_render_ad();

Monday, March 9, 2009

Open folder to location of a file

I cannot seem to remember this even though I wrote code at least twice to do it!

In dlgUpgrade From in DRC

'WIN32 API
Private Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


'Add functions
Public Function StartDoc(ByVal fileName As String, Optional CommandLine As String = "") As Long
StartDoc = ShellExecute(0&, "Open", fileName, CommandLine, vbNullString, 1)
End Function
Private Function GetParentDir(ByVal myFile As String, Optional RemoveSeparator As Boolean = True, Optional IgnoreDirValidation As Boolean = False) As String
On Error Resume Next
Dim i As Long, j As Integer
If RemoveSeparator = True Then
j = 1
Else
j = 0
End If
If IgnoreDirValidation = True Then
i = InStr(1, StrReverse(myFile), "\")
If i = 0 Then
GetParentDir = ""
Else
GetParentDir = StrReverse(Mid$(StrReverse(myFile), i + j))
End If
Else
If Dir$(myFile) = "" Then
GetParentDir = ""
Else
i = InStr(1, StrReverse(myFile), "\")
If i = 0 Then
GetParentDir = ""
Else
GetParentDir = StrReverse(Mid$(StrReverse(myFile), i + j))
End If
End If
End If
End Function

Private Function getFileName(filePath As String, Optional separator As String = "\", Optional removeExtension As Boolean = False) As String
Dim tmp As String
tmp = Mid$(filePath, InStrRev(filePath, separator) + 1)
If tmp = filePath Then
getFileName = ""
Else
If removeExtension Then
If InStrRev(tmp, ".") > 0 Then
getFileName = Left$(tmp, InStrRev(tmp, ".") - 1)
Else
getFileName = tmp
End If
Else
getFileName = tmp
End If
End If
End Function


'Call to OpenFileFolder
Private Sub OpenFileFolder(filePath As String)
Dim i As Long, j As Long, fileName As String
If isFileExist(filePath) Then
i = StartDoc("explorer.exe ", GetParentDir(filePath))
j = SetActiveWindow(i)
Sleep 1000 '<< 1 second
DoEvents

fileName = getFileName(filePath)
For i = 1 To Len(fileName)
SendKeys Mid$(fileName, i, 1)
DoEvents
Next i
End If
End Sub

Monday, March 2, 2009

Stupid SQL Tricks

http://www.sqlservercentral.com/articles/Administering/procedurecache/591/

Do not use sp_MyProc. SQL server will look for the stored procedure in Master database first. Name the procedure spMyProc and it does not happen.