C#: Base64 Encode a String

C# code to encode a string to its equivalent string representation that is encoded with base-64 digits.
private string EncodeBase64(string s)
                byte[] ascBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(s);
                return System.Convert.ToBase64String(ascBytes);
            catch (Exception e) { throw new Exception(e.Message); }


C#: Save a Form’s Size and Location

In an attempt to enhance user experience it may be desirable to save the size and location of a Form. When dealing with customizable user settings, it is important to determine where to persist the user’s applications settings. The Application class has a UserAppDataRegistry property that references the HKEY_CURRENT_USER\Software\[Control.CompanyName]\[Control.ProductName]\[Control.ProductVersion]. registry key in the user’s profile (If this key does not exist it is created). Here is a basic example of storing a form's state in the user's registry profile:
	private void Form_Load(object sender, EventArgs e)
            this.WindowState = (FormWindowState)FormWindowState.Parse(WindowState.GetType(), Application.UserAppDataRegistry.GetValue("WindowState", FormWindowState.Normal).ToString());
            if (this.WindowState == FormWindowState.Normal)
                int x = (int)Application.UserAppDataRegistry.GetValue("LocationX");
                int y = (int)Application.UserAppDataRegistry.GetValue("LocationY");
                this.DesktopLocation = new Point(x, y);
                int w = (int)Application.UserAppDataRegistry.GetValue("WindowSizeW");
                int h = (int)Application.UserAppDataRegistry.GetValue("WindowSizeH");
                this.Size = new Size(w, h);

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
            Application.UserAppDataRegistry.SetValue("WindowState", this.WindowState);
            Application.UserAppDataRegistry.SetValue("WindowSizeH", this.Size.Height);
            Application.UserAppDataRegistry.SetValue("WindowSizeW", this.Size.Width);
            Application.UserAppDataRegistry.SetValue("LocationX", this.DesktopLocation.X);
            Application.UserAppDataRegistry.SetValue("LocationY", this.DesktopLocation.Y);


SQL: Convert Integer to DateTime

If presented with a date represented as an integer in the format of 'YYYYMMDD', you can convert the number to a date with the following:

declare @_date as integer
set @_date = 20110910



SQL: Get Day of Week (Transact-SQL)

To retrieve day of week information using T-SQL you can use the DATENAME and DATEPART functions. The DATENAME function returns a character string of the datepart of a date. The DATEPART function returns an integer of the datepart of a date.

	DATENAME(dw,GETDATE()) [Day Name]     -- returns the day of week name	
	,DATEPART(dw,GETDATE()) [Day Number]  -- returns the day of week number
	,DATENAME(dy,GETDATE()) [Day of Year] -- returns the day of year
	,DATEPART(wk,GETDATE()) [Week Number] -- returns the week number of the year
	, GETDATE() [Todays Date]


Transact SQL results


SQL: Rename a database with TSQL

You can rename a SQL database using the SQL Server Management Studio (SSMS), however there may be times that you do not have access SSMS or would like to script the task.

The following TSQL will rename a SQL Database:


 ALTER DATABASE <database name> MODIFY NAME = <new database name>


Replace the and values with your database names. Note that the database must be in SINGLE_USER mode in order to complete this ALTER DATABASE command. In the code above, the set option WITH ROLLBACK IMMEDIATE tells the SQL Server that if it can’t complete the command right away, then the other pending transactions should be rolled back. This is helpful if there are automatic connections being made to the database that are difficult to KILL.

The following script can also be used to rename a database:


use [master]
  declare @olddb sysname
  declare @newdb sysname
  declare @cmd varchar(max)
  set @olddb = 'OldDbName' -- set the existing database name here
  set @newdb = 'NewDBName' -- set the new database name here
  set @cmd = 'ALTER DATABASE ' + @olddb + ' SET SINGLE_USER' -- you can add options to the command
  print @cmd
  exec (@cmd)
  set @cmd = 'ALTER DATABASE ' + @olddb + ' MODIFY NAME = ' + @newdb
  print @cmd
  exec (@cmd)
  set @cmd = 'ALTER DATABASE ' + @newdb + ' SET multi_user'
  print @cmd
  exec (@cmd)