Wednesday, November 17, 2010

ASP.NET with SQL 2005 Cache Dependence

1) Check to see Service Broker is enabled

SELECT name, is_broker_enabled FROM sys.databases

2) To enable the Service Broker on your database

ALTER DATABASE Pubs SET ENABLE_BROKER
GO

3) Inform SQL Server that the user running IIS has permission to subscribe to query notificatoins.

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO "IIS_SERVER\ASPNET"

4) Create a connectionStrings in the web.config

<connectionstrings>
<add name="myDbConnectionString" connectionstring="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True" providername="System.Data.SqlClient">
</connectionstrings>

5) Enable the website to be able to receive notifications.

protected void Application_Start(object sender, EventArgs e)
{
SqlDependency.Start(ConfigurationManager.ConnectionStrings["myDbConnectionString"].ConnectionString);
}

The following code also stops the listener:

protected void Application_End(object sender, EventArgs e)
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["myDbConnectionString"].ConnectionString);
}

6) Sets up the cach dependency. Example code.

string tableName = query.Context.Mapping.GetTable(typeof(T)).TableName;
List result = HttpContext.Current.Cache[tableName] as List;

if (result == null)
{
using (SqlConnection cn = new SqlConnection(query.Context.Connection.ConnectionString))
{
cn.Open();
SqlCommand cmd = new SqlCommand(query.Context.GetCommand(query).CommandText, cn);
cmd.Notification = null;
cmd.NotificationAutoEnlist = true;
SqlCacheDependencyAdmin.EnableNotifications(query.Context.Connection.ConnectionString);
if (!SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(query.Context.Connection.ConnectionString).Contains(tableName))
{
SqlCacheDependencyAdmin.EnableTableForNotifications(query.Context.Connection.ConnectionString, tableName);
}

SqlCacheDependency dependency = new SqlCacheDependency(cmd);
cmd.ExecuteNonQuery();

result = query.ToList();
HttpContext.Current.Cache.Insert(tableName, result, dependency);
}
}

Friday, November 5, 2010

Re-Associate the dbo to sa login after db restored

You could use the following SQL script to relink the dbo user of a db to login.

ALTER AUTHORIZATION ON DATABASE::[db name] TO [login name]

Wednesday, October 27, 2010

Tuesday, October 26, 2010

Required components of IIS7.0 for installing SQL Server 2005 on Windows 7

When you try to install SQL Server 2005 on Window 7, you may receive the following warning message for the IIS Feature Requirement item on the System Configuration Check page in the SQL Server 2005 Setup program:

Microsoft Internet Information Services (IIS) is either not installed or is disabled. IIS is required by some SQL Server features. Without IIS, some SQL Server features will not be available for installation. To install all SQL Server features, install IIS from Add or Remove Programs in Control Panel or enable the IIS service through the Control Panel if it is already installed, and then run SQL Server Setup again. For a list of features that depend on IIS, see Features Supported by Editions of SQL Server in Books Online.

This problem occurs because not all the IIS 7.0 components that SQL Server depends on are installed on the computer. The following table lists the affected components.

Component
Folder
Static ContentCommon HTTP Features
Default DocumentCommon HTTP Features
HTTP RedirectionCommon HTTP Features
Directory BrowsingCommon HTTP Features
ASP.NetApplication Development
ISAPI ExtensionApplication Development
ISAPI FiltersApplication Development
Windows AuthenticationSecurity
IIS MetabaseManagement Tools
IIS 6 WMIManagement Tools

Monday, March 29, 2010

Add Date Time stamp to the Windows Backup File Name

1. Use the Windows Backup GUI interface to create a backup job. Make sure to
specify that it should not run now but later.

2. Open the Task Scheduler, then open the backup job that you just created.

3. Mark & copy the whole command line.

4. Create the new batch file "MyBackup.bat"

5. Paste the backup command line into it.

6. Create some blank lines at the top of the file, then paste the following
code into the empty space.

@echo off
set MyDate=%date:/=_%_%time::=_%
ntbackup /.. /.. /.. /F d:\Backups\%MyDate%_File_Backup.bkf

7. Adjust the last line of the above code so that it matches your own
switches, then remove the original Windows Backup command line.

8. Use the Task Scheduler to create a task that will invoke
"MyBackup.bat" instead of your previous Windows Backup command

Tuesday, February 23, 2010

SQL 2005 : Database Mail Setup

  1. Enable Database Mail feature via Surface Area Configuration for Features.
  2. In SSMS, Use Database Mail Configuratin Wizard to create account and profile.
  3. Select a default profile.
  4. Create Operator.
  5. If you would use Alert to notifiy operator, enable Mail Profile in Alert system option of SQL Server Agent.
  6. Restart the SQL Server Agent.
  7. You are ready to use Database Mail to notify the operator now.

ASP.NET Page Events Lifecycle

This is for my own reference purpose. The The original info comes from : http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

When using master pages, the normal page event lifecycle is a little different. Here is the actual order:

  1. Page.OnPreInit
  2. MasterPageControl.OnInit (for each control on the master page)
  3. Control.OnInit (for each contol on the page)
  4. MasterPage.OnInit
  5. Page.OnInit
  6. Page.OnInitComplete
  7. Page.LoadPageStateFromPersistenceMedium
  8. Page.LoadViewState
  9. MasterPage.LoadViewState
  10. Page.OnPreLoad
  11. Page.OnLoad
  12. MasterPage.OnLoad
  13. MasterPageControl.OnLoad (for each control on the master page)
  14. Control.OnLoad (for each control on the page)
  15. OnXXX (control event)
  16. MasterPage.OnBubbleEvent
  17. Page.OnBubbleEvent
  18. Page.OnLoadComplete
  19. Page.OnPreRender
  20. MasterPage.OnPreRender
  21. MasterPageControl.OnPreRender (for each control on the master page)
  22. Control.OnPreRender (for each control on the page)
  23. Page.OnPreRenderComplete
  24. MasterPageControl.SaveControlState (for each control on the master page)
  25. Control.SaveControlState (for each control on the page)
  26. Page.SaveViewState
  27. MasterPage.SaveViewState
  28. Page.SavePageStateToPersistenceMedium
  29. Page.OnSaveStateComplete
  30. MasterPageControl.OnUnload (for each control on the master page)
  31. Control.OnUnload (for each control on the page)
  32. MasterPage.OnUnload
  33. Page.OnUnload

Friday, February 19, 2010

Steps to install dotProject

Add "dp_user" user to the MySQL.
1) Install Apache web server.

2) Install PHP to your web server.

PHP should support GD and MySQL

3) Install MySQL.

4) Add "dp_user" user to the MySQL.

5) Create a schema (Database) "dotproject" and assign user "dp_user" has full permission of this DB.

6) Copy all dotProject files to the htdocs folder.

- Open browser to access http://Your Domain/dotProject/install/index.php

- follow the step to complete the installation.

7) The default admin login is :

User Id : admin
Password : passwd

Prepare SQL 2005 to use CLR

1) Turn on the 'clr enabled" feature of your server.

sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO

2) Turn on the Trustworthy property of your database if your clr assembly require access external resources.

ALTER DATABASE [DB Name]
SET TRUSTWORTHY ON