NetInverse Developers Blog

March 7, 2009
Category: SQL — Tags: — admin @ 11:02 pm

SQL 2005 has introduced a new system database referred as Resource database, namely RDB. Its mdf file name is ‘mssqlsystemresource.mdf’. Resource database is a read-only database that contains all the system objects that are included with SQL Server 2005. All system objects such as sys.objects are physically stored in the Resource database, but they logically appear in the sys schema of every database.

You can run

    SELECT [name], object_definition(object_id) from sys.system_objects

from any database, which will give a same output.

Category: SQL — Tags: — admin @ 10:53 pm

The master database stores your logins and, most importantly, the pointers to all of your databases. Without the master database, you can’t successfully start SQL Server. To recover your master database, you need to use the Rebuild Wizard (Rebuildm.exe), located in the \Program Files\Microsoft SQL Server\80\Tools\BINN directory.

However, you may not know that RebuildM.exe utility has retired since SQL 2005. In case your system database gets corrupted, how you rebuild Master database?

You will need to use the setup.exe on the installation CD to rebuild system databases. RebuildM.exe now has been merged into the main setup program.

Category: PowerShell — Tags: — admin @ 10:45 pm

Have you ever writen any BAT file or Perl script before? Now it’s time to use Windows PowerShell instead! It’s a powerful tool for writing automation scripts like build or pre-processing, etc. It’s extremely fun to use it since it’s .Net based and supports Xml and objects.

Microsoft Windows PowerShell command line shell and scripting language helps IT Professionals achieve greater productivity. Using a new admin-focused scripting language, more than 130 standard command line tools, and consistent syntax and utilities, Windows PowerShell allows IT Professionals to more easily control system administration and accelerate automation. Windows PowerShell is easy to adopt, learn, and use, because it works with your existing IT infrastructure and existing script investments, and because it runs on Windows XP, Windows Vista, Windows Server 2003 and Windows Server Longhorn. Exchange Server 2007, System Center Operations Manager 2007, System Center Data Protection Manager V2, and System Center Virtual Machine Manager leverage Windows PowerShell to improve efficiency and productivity.

In short: PowerShell

  • exposes the power of .NET via a command line environment and scripting language
  • allows you to pipe objects from one command to another!
  • provides powerful supports of XML handling and regular expressions

    Get Started:

    Download Powershell from Microsoft first. Write and run a script. You can run following command to allow you to run unsigned scripts on your local machine.

            PS C:\> Set-ExecutionPolicy RemoteSigned

    To create a script, just make a text file with the .ps1 extension, like MyBuild.ps1. To run it, you need to type the full path to the script or use .\filename, like .\MyBuild.

    Download info for Powershell.

  • Category: .Net — Tags: , — admin @ 10:32 pm

      Can you get the address of a Managed Type? look at the code below:

        
        class Point
        {
            public int x;
            public int y;  
        }
      
        class A  
        {
            unsafe static void Main()   
            {
                Point pt = new Point();
                // Using fixed allows the address of pt members to be
                // taken, and "pins" pt so it isn't relocated.
                fixed (int* p = &pt.x) //This is okay!
                {
                    *p = 1;
                }
    
                fixed (int* p = &pt) //This won't compile.  
                {
    
                    //...
                }  
            }
        }

     Above code won’t compile due to following error:

    Cannot take the address of, get the size of, or declare a pointer to
    a managed type ('something.Point')

    Conclusion: from above sample, you can see that you cannot get the address of a managed type!  But you can point to value types of a class.

    By the way, you might don’t know much about fixed statement, I copied the definition of it from MSDN.

    fixed Statement (C# Reference)

    The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context.

    March 6, 2009
    Category: Design Pattern — Tags: — admin @ 11:03 pm

    Please check out Scott Bain’s http://www.netobjectivesrepository.com. This is a site where you can find authoritative information about design patterns.

    This respository is sponsored by Net Objectives, a Seattle-based organization dedicated to training, coaching, and consulting on software design, agile methodologies, test-driven development, lean software process, and scrum.

    You are free to use this material for your edification and study, and, optionally, you may contribute your views on patterns and forces in software development by signing up for a free membership. For other online resources provided by Net Objectives, please visit Net Objectives Online. The site manager is Scott Bain.

    Knowing the OO basics doesn’t make you a good OO designer. You need to know OO principles and Patterns. Following is cited from the book Head First Design Patterns.

    OO Basics

    • Abstraction
    • Encapsulation
    • Polymorphism
    • Inheritance

    OO Principles

    • Encapsulate variation
    • Favor composition over inheritence
    • Program to interfaces, not implementations

    OO Patterns

    • Strategy- defines a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
    • Bridge…
    March 5, 2009
    Category: SQL — Tags: , , — admin @ 9:41 pm

    Dynamic(Ad hoc) SQL query can be as performant as stored procedures if you use it correctly. SQL server compiles it, caches the compiled query plan and re-uses it next time. If not, SQL server may have to re-compile the query every time, and which can be sometimes 100 times slower!

    For example:

         String query = @"SELECT *
                            FROM employees e
                      INNER JOIN EmployeeDetails et
                              ON e.employeeid = et.employeeid
                           WHERE e.employeeid = @id";
    
         sqlCmd.CommandType = System.Data.CommandType.Text;
         SqlParameter idParam = new SqlParameter(
             "@id", System.Data.SqlDbType.Int, 0);
         sqlCmd.Parameters.Add(idParam);
         sqlCmd.Parameters[0].Value = 100;
         sqlReader = sqCmd.ExecuteReader();
    

    If you use SQL Tracer, you will see the SQL server executes following statement:

     EXEC sp_executesql N'SELECT * FROM employees e
     INNER JOIN EmployeeDetails et ON e.employeeid = ee.employeeid
     WHERE e.employeeid = @id', N'@id int',@id=100

    In this case, SQL server can cache the query plan and re-used it effectively. Because the statement is parameterized, even the value of @id changes, the compiled statement remains same and can be always re-used.

    A common mistake of using dynamic SQL is like below:

          String query = @"SELECT *
                             FROM employees e
                       INNER JOIN EmployeeDetails et
                               ON e.employeeid = ee.employeeid
                            WHERE e.employeeid = " + id.ToString();
    
         sqlCmd.CommandType = System.Data.CommandType.Text;
         sqlReader = sqCmd.ExecuteReader();

    If you trace again, this time you will see SQL server executes the following instead:

     SELECT * FROM employees e INNER JOIN EmployeeDetails et
     ON e.employeeid = ee.employeeid WHERE e.employeeid = 100

    SQL server can cache and re-use above statement as well. However, since the statement is not parameterized, if the
    employeeid changes, SQL server will have to re-compile the statement. The consequence is low cache hit and SQL server will have a lot of recompiling, the overall performance will be poor.

    So the take away here is, you can use dynamic SQL query, but do use SqlParameter to make your dynamic SQL statement parameterized.

    March 4, 2009
    Category: .Net — Tags: , , , — admin @ 11:03 pm

    Please read the following sample code with Monitor.Enter and Monitor.Exit. Does the lock work?

    class LockMe
    {
        private static int counter = 0;
        public void Lock()
        {
           Monitor.Enter(counter);
           counter++;
           Monitor.Exit(counter);
        }

    No! The code actually is totally wrong. You are not supposed to lock a value type, which doesn’t have a SyncBlock field. Monitor.Enter(counter) will cause an int(counter) to be boxed first and then you are getting the lock from a box object, not the int itself. The lock won’t work as you expected, since a different object(a different SyncBlock) will be used each time.

    « Newer Posts

    ©2009 NetInverse. All rights reserved. Powered by WordPress