safish.com

home of a small fish

08 February 2012 at 01:19

I'm perpetually restoring databases, and doing it via the GUI using SQL Server Management Studio is tedious. Instead, I like to use a script to restore my database. This involves the creation of a .bat file and an accompanying .sql file, as follows:

The batch file (e.g. restore.bat)

There are three things to note here that you may need to change:

  1. The SQL Server instance
  2. The database name - in my example 'MyDatabase'
  3. The name of the accompanying .sql file (in my example called restore.sql). This assumes the .bat and .sql files are in the same database

sqlcmd -S localhost\SQL2008r2 -i restore.sql -v database=MyDatabase -v root="%CD%"
PAUSE

The sql file (e.g. restore.sql)

There are a few things to note here:

  1. The name of the backup file, in my example MyDatabase.bak - the script assumes this is also in the same folder
  2. The names of the files associated with your database. These may differ on the machine where the database was backed up, to the machine where you are restoring the database to. You can get these names from the original database in Management Studio by right-clicking the database and viewing properties, click Files, and checking the Logical Name column. If you don't know these, just put any old value in and you will receive an error when running the script that should show the correct name. In my example the data file is MyDataFile, and the log file is MyLogFile_log
  3. The user you want to grant db_owner access to. This user must exist as a login on the database instance. In my example, it is called 'myuser'. You may want to change the roles assigned to the user, I am just assigning the db_owner role.

USE MASTER
GO

DROP DATABASE $(database)
GO

RESTORE DATABASE $(database) FROM DISK = '$(root)\MyDatabase.bak'
WITH MOVE 'MyDataFile' TO 'c:\temp\MyDatabase.mdf',
MOVE 'MyLogFile_log' TO 'c:\temp\MyDatabase.ldf'
GO

USE $(database)
GO

sp_grantdbaccess 'myuser'
GO

sp_addrolemember 'db_owner', 'myuser'
GO
06 February 2012 at 12:22
Exporting an SVN repository to an Hg repo is dead easy with the latest version of TortoiseHg:
  1. Open Workbench, and click File...Settings...Extensions
  2. Select the "convert" checkbox and restart work bench
    hg convert extension
  3. From the command prompt, run "hg convert http://<yoursubversionrepo/> <yourdestinationfolder>", and you're done!
03 January 2012 at 13:11
Some useful jQuery plugins:
  • DropKick - superb replacement for standard drop-downs, with in-built graceful degradation
  • FlexSlider - awesome slider plugin for image display
  • Apprise - a nice little replacement for alert/confirmation boxes, although I\'m not sure how well this works across platforms. It doesn\'t look great on IE7/8.
  • Reveal - very nice modal dialog implementation
03 December 2011 at 07:35
This is the start of my own technical blog. I'll be shifting my current tech blog (http://salmontech.blogspot.com) to this location over the next few months, as well as restoring posts from the blog I had before that.
04 October 2004 at 00:00

Caching - File Dependancies

You can output the contents of a file from the cache, and ASP.NET provides the facility to clear the cache whenever the contents of the file change. For example:

  DataSet ds = (DataSet)Cache["MyInfo"];
  if (ds == null) {
    ds = new DataSet();
    ds.ReadXml(MapPath("MyInfo.xml"));
    Cache.Insert("MyInfo", ds, Caching.CacheDependency(MapPath("MyInfo.xml")));
  }
  // do whatever you want with the dataset here...
  DropDownList1.DataSource = ds;  ...

Output Caching

As per the microsoft documentation, pages can be cached on the server with the following:

  <%@ OutputCache Duration="60" VaryByParam="None" Location="Server: %>

This can also be cleared on another page (if someone changes the values to be shown on an item, for example), using the static RemoveOutputCacheItem method.

If you want to clear the output cache for "http://yourserver/webSiteRoot/cachedFile.aspx", then in your page that does the clear call, do the following:

  HttpResponse.RemoveOutputCacheItem("/webSiteRoot/cachedFile.aspx"); 

Output Caching - Clearing using Cache Dependencies

An easier way to remove output caching from multiple pages is to attach a cache dependency on those pages. In the page that is actually cached:

  string cacheKey = "mypage.aspx?" + parameters; 
  Cache[cacheKey] = new object(); 
  Response.AddCacheItemDependency(cacheKey); 

Then, on the page that invalidates the cache (for example, an admin page that inserts/updates database records):

  string cacheKey = "mypage.aspx?" + parameters; 
  Cache.Remove(cacheKey); 
08 September 2004 at 00:00
In Windows 9x, the default amount of environment space provided to MS-DOS windows is often too small to run many batch files. To increase the amount of environment space available, do the following:
  1. Click Start, Run and enter sysedit
  2. In the C:CONFIG.SYS window, add the line:
    SHELL=C:COMMAND.COM /E:4096 /P
    
  3. Reboot your system. This should be enough to run most batch files.
04 April 2004 at 00:00
The terminator symbol marks the starting or ending point of the system. It usually contains the word "Start" or "End."
A box can represent a single step ("add two cups of flour"), or and entire sub-process ("make bread") within a larger process.
A printed document or report.
A decision or branching point. Lines representing different decisions emerge from different points of the diamond.
Represents material or information entering or leaving the system, such as customer order (input) or a product (output).
Indicates that the flow continues on another page, where a matching symbol (containing the same letter) has been placed.
Lines indicate the sequence of steps and the direction of flow.
08 April 2003 at 00:00
To run command-line queries on a PostgreSQL database running on Linux, you need to locate the psql executable, connect to the database and run queries manually:
locate psql
(e.g. returns /usr/bin/psql)
/usr/bin/psql <database_name>
SELECT * FROM <table> LIMIT 200;
q (to quit)
21 February 2003 at 00:00
Converting binary to decimal

Given the binary value 1000:

1000 
  = 1 * 23 + 0 *22 + 0 * 21 + 0 * 20
    = 23
      = 8 (decimal)

Converting decimal to binary

Given the decimal value 10:

10
  = 23 + 21 
    = 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 
      = 1010 (binary)
An easier way to work this out is, for example (given a decimal number of 11): divide the decimal number by 2, recording the quotient and remainder in the next row, rounding the quotient DOWN each time, until you have a quotient of 0.

  Quotient Remainder  
  11    
Repeat the procedure - divide by 2, record quotient and remainder in the next row 5 1 11/2 = 5 remainder 1
 Repeat again 2 1 5/2 = 2 remainder 1
 Repeat again 1 0 2/2 = 1 remainder 0
 Stop!  Quotient = zero 0 1 1/2= 0 remainder 1

Result: (read from bottom up) Decimal value 11 = binary 1011

20 February 2003 at 00:00

Mounting a folder on an NT Machine visible on the Network Neighbourhood

  mkdir /mnt/targetfolder
  /usr/bin/smbmount //NTMachineName/folder /mnt/targetfolder -o username=xxx,password=yyy