Friday, August 14, 2009

SQL - How to rename a DATABASE in MS SQL Server 2005/2008

You could change the DATABASE name by calling sp_renameDB system stored procedure but it will be deprecated in future versions.
EXEC sp_renameDB 'myOldDB','myNewDB'


sp_renamedb (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms186217.aspx

According to MSDN document in above URL you better Use ALTER DATABASE MODIFY NAME Instead of sp_renameDB to rename.
"This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER DATABASE MODIFY NAME instead. For more information, see ALTER DATABASE (Transact-SQL)."

ALTER DATABASE (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms174269.aspx

/* Rename the Database myOldDB to myNewDB */
ALTER DATABASE myOldDB MODIFY NAME = myNewDB
GO


But none of above mentioned commands change Physical Name of database files, how can we change Physical file names of MS SQL Database files (DB and Log files) ? it comes in next blog entry, cheers.
Share/Bookmark

Sunday, August 9, 2009

XSD - XML Schema constraints and value checks


Share/Bookmark

Friday, August 7, 2009

Valid XHTML document


An XHTML document is technically an XML document. XML documents can include an XML declaration as the first line in the document, something like this:
<?xml version="1.0" encoding="UTF-8"?>

There are only a few major rules to consider, but you have to follow them if you want to create a valid XHTML document. Here they are in brief:

* In an XHTML document every tag must be closed.
* Empty elements without content, must be correctly closed with a closing slash.
For example, a break tag is formatted <br />.
* Tags must be nested correctly,
it works like a LIFO queue, the last tag you open must be closed first.
* All XHTML tags should be in lowercase.
* All attribute values are enclosed in quotation marks.
* A valid XHTML document needs a valid XHTML DOCTYPE declaration.

The DOCTYPE declaration works for several purposes:
* DOCTYPE helps your page to be validated as XHTML.
* DOCTYPE allows the browser knows the version of your page markup language.
* DOCTYPE references the specific DTD for your page markup language.
* DOCTYPE enables your page to be displayed properly in standard web browsers
(IE, Netscape Navigator, Mozilla, Firefox, Opera, Chrome, and ...).

For an XHTML 1.0 document, you can choose one of three different DOCTYPES:
- strict
- transitional
- frames

- strict DOCTYPE declaration:
If for presenting and styling your document you are using CSS.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

- transitional DOCTYPE declaration:
If your document includes any presentational or styling markup code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

- frames DOCTYPE declaration:
If your document is in frames.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

The easiest to use is the transitional DOCTYPE declaration, but using CSS to present document sometimes is the best way.

After XHTML DOCTYPE declaration, it must have an additional line of markup:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

In XHTML, the above line works instead of the opening <html> in HTML document. It adds information about the XHTML namespace. (A namespace is basically a vocabulary of elements and attributes)
Share/Bookmark

Monday, August 3, 2009

.NET - Can I remove .NET Framework 1.0 or 1.1 now that I have 2.0 or later on my machine?


Yes, you can! cheeers, it's because of side by side execution manner in earlier versions (1.0, 1.1, 2.0). I keep it, cuz it doesn't occupy too much space.

- But there are some breaking changes in .NET Framework 2.0 if you have an application which wants to run on version 1.0 or 1.1 if you remove those versions.

Breaking Changes in .NET Framework 2.0
http://msdn.microsoft.com/en-us/netframework/aa570326.aspx

- Look at the .NET Framework 3.5 Architecture,:
http://msdn.microsoft.com/en-us/library/bb822049.aspx

"The relationship of the .NET Framework versions 2.0, 3.0, and 3.5 differs from the relationship of versions 1.0, 1.1, and 2.0. The .NET Framework versions 1.0, 1.1, and 2.0 are completely separate from each other, and one version can be present on a computer regardless of whether the other versions are present. When versions 1.0, 1.1, and 2.0 are on the same computer, each version has its own common language runtime, class libraries, compiler, and so forth. Application developers can choose which version to target."

- The .NET Framework 2.0 was a generational release over the .NET Framework 1.0 but .NET Framework 3.0 (and later) adds new technologies (additive release).

How .NET Framework 3.0 Relates to .NET Framework 2.0 and Earlier
http://msdn.microsoft.com/en-us/library/aa480198.aspx#netfx30_topic3

"The .NET Framework 3.0 adds new technologies to the .NET Framework 2.0, which makes the .NET Framework 3.0 a superset of the .NET Framework 2.0. You can think of .NET Framework 3.0 as an "additive" release to the .NET Framework 2.0, as contrasted with a generational release where software is revised across the board. (For example, the .NET Framework 2.0 was a generational release over the .NET Framework 1.0.)

Because .NET Framework 3.0 is an additive release and uses the core run-time components from .NET Framework 2.0, it is completely backward compatible with the earlier version. Your existing .NET Framework 2.0 based-applications will continue to run without any modifications and you can safely continue your investments using the technologies that shipped with .NET Framework 2.0."

The same question in social.msdn: Which version of Framework .net?
http://social.msdn.microsoft.com/Forums/en-US/netfxsetup/thread/0387a1d6-404b-48d4-9d8c-53aefcb75d7b

.NET Framework article in WiKiPedia is also interesting:
http://en.wikipedia.org/wiki/.NET_framework

Share/Bookmark

Sunday, August 2, 2009

One year Blog anniversary


It's a happy day, one year ago when I started this blog, I wanted to share something with others as others shared something with us. I'm happy to be part of this unfinished sharing story, then let's share more.
Share/Bookmark

Friday, July 31, 2009

C# - C++ - [StructLayout] to pass structures to unmanaged function

CLR rearranges the order of type members during runtime, it generates better performance, faster access to members, less memory consumption, and so. But when .NET wants to interoperate with unmanaged codes, it's necessary to keep the structure layout as is. For this purpose, it's possible to specify the layout of a structure by using the StructLayout attribute.

[StructLayout(LayoutKind.Sequential)] keeps the sequence of members in structure type as defined. The following sample gets the OS version by passing a pointer to structure to GetVersionEx function in unmanaged kernel32.dll:

...
using System.Runtime.InteropServices;
...
[DllImport("kernel32.dll")]
public static extern
bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo);

[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFO
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public String szCSDVersion;
}
...
OSVERSIONINFO osVer = new OSVERSIONINFO();
osVer.dwOSVersionInfoSize = Marshal.SizeOf(osVer);
string osVersion = string.Empty;

if (GetVersionEx(ref osVer))
osVersion = string.Format("{0}.{1}.{2}",
osVer.dwMajorVersion,
osVer.dwMinorVersion,
osVer.dwBuildNumber);

MessageBox.Show("OS Ver: " + osVersion);

OSVERSIONINFO Structure
http://msdn.microsoft.com/en-us/library/ms724834(VS.85).aspx
Share/Bookmark

CardSwap !


I received an email from a friend about cardswap.ca, perfect place to swap or sell your gift cards or buy one with discount. It's just started but I expect it grows pretty fast.

CardSwap is the premier online Canadian marketplace for gift card exchanges. Whether you want to buy gift cards at substantial savings, or sell them for cash, CardSwap offers the simplest way to get what you want.
http://www.cardswap.ca/
Share/Bookmark

Thursday, July 30, 2009

C# - How can we show Markup Language (HTML, XML, XSD, ... ) codes in blogger ?

Well, we need to encode ampersands and angle brackets:
< to &lt;
> to &gt;
& to &amp;

I use a great tool in the following url:
http://centricle.com/tools/html-entities/
Encode / Decode HTML Entities
Convert text to HTML entities (and vice-versa)
Share/Bookmark

Monday, July 27, 2009

C# - Validate XML document against a Schema - XmlReaderSettings

Sample code for Validate XML document against a Schema:

...
using System.Xml;
using System.Xml.Schema;
...

public static bool XmlValidateByXsd(string xmlFile, string xsdFile)
{
bool retVal = false;
if( ! File.Exists( xmlFile ) || ! File.Exists( xsdFile ) )
return retVal;

try
{
XmlReaderSettings xmlRdrSettings = new XmlReaderSettings();
xmlRdrSettings.ValidationType = ValidationType.Schema;

xmlRdrSettings.Schemas = new XmlSchemaSet();
xmlRdrSettings.Schemas.Add(null, xsdFile);
xmlRdrSettings.ValidationEventHandler +=
new ValidationEventHandler(ValidationEventHandler);
using (XmlReader xmlRdr = XmlReader.Create(xmlFile, xmlRdrSettings))
{
while (xmlRdr.Read()) { }
}
retVal = true;
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}

return retVal;
}

private static void ValidationEventHandler(object sender, ValidationEventArgs args)
{
// Do error handling
// Console.WriteLine("Error: " + args.Message);
}


The code line to call the method to validate XML against XSD:

bool retVal = myClass.XmlValidateByXsd(@"C:\employee.xml", @"C:\employee.xsd");


Sample XML file:

<?xml version="1.0" standalone="yes"?>
<Employees xmlns="http://iborn2code.blogspot.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://iborn2code.blogspot.com employee.xsd">
<Employee>
<SIN>525252525</SIN>
<FirstName>AB BA</FirstName>
<City>Vancouver</City>
<Zip>V1V</Zip>
</Employee>
<Employee>
<SIN>525252527</SIN>
<FirstName>BC CB</FirstName>
<City>Kelowna</City>
<Zip>V2V</Zip>
</Employee>
</Employees>


Sample XML Schema for above XML:

<div class="mycode">
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="SIN" type="xs:string"/>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="Zip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>
</div>

Share/Bookmark

Thursday, July 23, 2009

C# - Error Message - 'This would cause two bindings in the collection to bind to the same property'

It shows the code is calling Control.DataBindings.Add two times to the same parameters.
May be duplicate binding to the DataBindings collection causes this error. Anyway one
solution is to clear Control.DataBindings before adding new binding:


this.txtAddress.DataBindings.Clear();
this.txtZipCode.DataBindings.Clear();

this.txtAddress.Text = string.Empty;
this.txtZipCode.Text = string.Empty;

this.txtAddress.DataBindings.Add("Text", myDtaTbl, "Address");
this.txtZipCode.DataBindings.Add("Text", myDtaTbl, "ZipCode");

Share/Bookmark