Skip to main content

Posts

Create a copy of database and renaming it using mdf & ldf files

If You want to use the same directory for your mdf\ldf files You will have to (physically) rename them. 1. Detach files from local PC 2. Rename mdf and ldf files 3. Copy\Move the files to the server directory where the original Database is stored. (In My example I used D:\Data for mdf and E:\Logs for ldf, change the path before running the script) 4. Use this code: USE [ master ] GO CREATE DATABASE [ myNewSite_db ] ON ( FILENAME = N 'D:\Data\myNewSite_db.mdf' ), ( FILENAME = N 'E:\Logs\myNewSite_db_log.ldf' ) FOR ATTACH GO

Difference Between String and StringBuilder

String are Immutable (Not Modifiable). If you try to modify the string it actually creates a new string and the old string will be then ready for garbage collection. StringBuilder when instantiated, creates a new string with predefined capacity and upto that capacity it can accodomate string without needing to create a new memory location for the string....i mean it is modifiable and can also grow as and when needed. When the string needs to be modified frequently, preferably use StringBuilder as its optimized for such situations.

Stored procedure slow when called from web, fast from Management Studio

Some time this problem occurs due to compiled query plan became corrupt, or invalid. Stored procedure slow when called from web, fast from Management Studio becuase Basically connections through Sql Server Management Studio (SSMS) by default have SET ARITHABORT ON. ADO.NET connections do not. The main difference when running with or without this setting, a different query plan is created for the both stored procedure calls. In case of ARITHABORT was OFF, the SSMS command would use the pre-compiled cached query plan that the ADO.NET connection was using (compiled query plan became corrupt, or invalid.), and therefore timeout. Use following Command to refresh the query Plans. DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE Query parameterization promotes the reuse of cached execution plans, thereby avoiding unnecessary compilations, and reducing the number of ad-hoc queries in the plan cache. This is called Parameter Snifting. Sql-server using Parameter Snifting (Default Enabled)...

C#, Read a file

C#, Code to read a file  String strHtmlMessage = String .Empty; String strFilePath = HttpContext.Current.Server.MapPath("~/Mails/Confirmationmaill.htm");   using (StreamReader sr = new StreamReader(strFilePath))   {     strHtmlMessage = sr.ReadToEnd();  }

Time on my EC2 windows instance changed automatically

Configure Windows time sync: First ensure that you can get time from a NTP server, otherwise the instructions below are not going to work. To test, get the current variance from an NTP server using the following Windows Time service command w32tm /stripchart /samples:5 /computer:pool.ntp.org /dataonly In a VPC we had to open UDP port 123 for inbound connections in the NAT security group. To configure NTP time sync in Windows 1. Registry edit - HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\TimeProviders\NtpClient Set SpecialPollInterval to polling interval in seconds (we set ours to 60). 2. Registry edit - HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Parameters Set Type to NTP 3. Run the following commands to configure the time servers (can use other servers, we're using 4 time servers - 0.pool.ntp.org, time.nist.gov, 1.pool.ntp.org, and 2.pool.ntp.org) net stop w32time w32tm /config /syncfromflags:manual /manualpeerlist:"0....

How to increase Session Timeout?

We can set our custom session timeout in our applications by following Methods 1.     In  Web.config  file we can set session timeout like as shown below  < configuration > < system.web >  < sessionState   mode = " InProc "   timeout = " 120 " >  </ sessionState >  </ system.web > </ configuration > Note that if you are using Forms authentication, the Forms timeout setting will log the user out after the set timeout period so you will also have to adjust this attribute: < authentication mode = " Forms "  >  < forms   timeout = " 120 " >  </ authentication > 2.     In  Global.asax  file we can set session timeout in Session_Start  event like this void Session_Start( object sender, EventArgs e) { // Code that runs when a new session is started Session....

Get size of all tables in database

Determining space used for each table in a SQL Server SELECT t.NAME AS TableName,     s.Name AS SchemaName,     p.rows AS RowCounts,     SUM (a.total_pages) * 8 AS TotalSpaceKB,     SUM (a.used_pages) * 8 AS UsedSpaceKB,     ( SUM (a.total_pages) - SUM (a.used_pages)) * 8 AS UnusedSpaceKB FROM sys.tables t INNER JOIN sys.indexes i ON t. OBJECT_ID = i. object_id INNER JOIN sys.partitions p ON i. object_id = p. OBJECT_ID AND i.index_id = p.index_id INNER JOIN   sys.allocation_units a ON p.partition_id = a.container_id LEFT OUTER JOIN   sys.schemas s ON t. schema_id = s. schema_id WHERE t.NAME NOT LIKE 'dt%' AND t.is_ms_shipped = 0 AND i. OBJECT_ID > 255 GROUP BY t.Name, s.Name, p.Rows ORDER BY t.Name