24 Apr 2013

Named String Format in C#

It is nice on how NET Framework gave a bunch of “shortcut” code, including String.Format. But we have to keep track of the indices in the object array which is not really nice.

82653205

It would be good if we just write code, declare a named string collection and format it.

  1. string format = "my name is [[name]] for it called [[name]] to be noted. and address is [[address]].";
  2. Hashtable ht = new Hashtable() { { "name", "Minion No. 10594" }, { "address", "gru's place" } };
  3. txtResult.Text = format.Format(ht);

Now we can use my commonly-used library in rdz.codeplex.com, and use the Format method in StringUtility class. You can download it and use it.

Don’t forget to add reference to Rdz.dll and put using directive as below. You are good to go and the Format function will be available on every string object, like in the example above.

  1. using Rdz.Utility;

In case you didn’t notice, the hashtable is case-sensitive. So to make it case-insensitive, declare Hashtable as below.

  1. Hashtable ht = new Hashtable(StringComparer.CurrentCultureIgnoreCase) { { "name", "Minion No. 10594" }, { "address", "gru's place" } };

Cool! See you! Winking smile

Important Command-Line for Developers

There some bunch of tools which we never know that will help us in our daily operations as developer. But I’ll give you some of that.

Generate Web-Service Proxy Class from WSDL

Generating proxy class from WSDL is important if you want to get data from multiple web services containing exactly the same method. So, in the end you can use the class, set the URL and call the method simultaneously with other URLs.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\WSDL.exe" /out:"<path to cs file>\<cs filename>.cs" /n:<namespace> http://<complete URL to WSDL>/<WebServiceName>.asmx?wsdl

Generate XSD from XML

Generating XSD can be done by using XSD.exe file located in Microsoft SDKs.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\XSD.exe" "<path to xml file>\<xml filename>.xml"

Generate Class from XSD

Generating Class from XSD also useful in many ways. There are 2 type of class, the one which we can use as dataset, and the one just plain class to use to read Xml. So if you want to deserialize an XML and put it on your code, definitely you will need to convert XML to XSD, and XSD to Class.

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\XSD.exe" /language:CS /classes /n:<namespace> "<path to XSD file>.xsd"

6 Apr 2013

Attach SQL Database from Network Drive

One crazy question came up in mind, can I attach a database from network drive? There’s a lot of recommendation, that we can’t attach SQL Database. Why would we want it? Anyway no one would do that, unless they want a little bit hiccup in their system and mess up the whole thing? That’s true, but I want a solid answer. Can it be done?

The answer is it can be done. But again, on really strict rules, this is not recommended. Why would I want it? Because I have development VM (VirtualBox) and I need those big databases (say about ~20GB) to be exist on my system without copying it inside my Virtual Hard Disk which will take some time to copy. Again, it’s a development VM, so I could manage it and no one would care if my system crashed or database not exist.

So, here it is.

Attach Database from Network
  1. -- this command to turn on network share attach DB
  2. DBCC TraceOn(1807)
  3. -- attach it
  4. EXEC sp_attach_db @dbname = N'WSS_Content_85',
  5.    @filename1 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85.mdf',
  6.    @filename2 = N'\\Vboxsvr\r\DOCS\SQL Database\WSS_Content_85_log.ldf'
  7. -- this command to turn off network share attach DB
  8. DBCC TraceOff(1807)

Please do note that this command would likely not recommended, especially on production server. Thanks to Sumardi Soemartopo for blogging about this COOL tips, and this is the link to that page http://guozspace.wordpress.com/2009/02/26/microsoft-sql-server-how-to-attach-database-from-network-drive/.