Gokul's Blog


Leave a comment

Log4net & Sqlite

Setup Log4net: http://www.justinrhinesmith.com/blog/2008/05/12/quick-and-easy-log4net-setup/

I have created a sample of using SQLite and log4net based on the configuration from log4net website.

DownloadCode

Advertisements


Leave a comment

SQL Scripts – Backup, Restore DB

Backup SQL DB

--Backup Script
declare @sFileName varchar(50), @dbName varchar(50), @sql varchar(1024)
set @dbName ='MyDatabase'
set @sFileName = 'C:\' + CONVERT(CHAR(8), GETDATE(), 112)+'_'+ @dbName +'.bak'
set @sql = 'BACKUP DATABASE ' + @dbName + ' TO DISK =  ' + @sFileName + ' WITH INIT'
exec (@sql)
print 'The requested db is backed  at the following file location'+@sFileName
--End of Backup script

Kill Connections to Database

--Kill Connection to the current db
declare @databaseName varchar(100), @execSql nvarchar(1000)
set @databaseName = 'test'

print 'Killing active connections to the "' + @databaseName + '" database'
-- Create the sql to kill the active database connections
set @execSql = ''
select @execSql = @execSql + 'kill ' + convert(char(10), spid) + ' '
from master.dbo.sysprocesses
where db_name(dbid) = @databaseName
and
DBID <> 0
and spid <> @@spid
exec (@execSql)

Restore Database (This was taken from internet)

declare @backupFileName varchar(100), @restoreDirectory varchar(100),
        @databaseDataFilename varchar(100), @databaseLogFilename varchar(100),
        @databaseDataFile varchar(100), @databaseLogFile varchar(100),
        @databaseName varchar(100), @execSql nvarchar(1000)
-- Set the name of the database to restore
set @databaseName = 'test'

-- Set the path to the directory containing the database backup
set @restoreDirectory = 'C:\tempp' -- such as 'c:\temp\'

-- Create the backup file name based on the restore directory, the database name and today's date
--//This will always get the previous days backup
set @backupFileName = @restoreDirectory + @databaseName + '_data_' 
+ replace(convert(varchar, getdate()-1, 112), '-', '') + '.dmp'
--(or) manually set ur filename as follows
set @backupFileName = 'C:\temp\20090318_Mydatabse.bak'
print 'BackupFileName' + @backupFileName

-- Get the data file and its path
select @databaseDataFile = rtrim([Name]),@databaseDataFilename = rtrim([Filename])
from master.dbo.sysaltfiles as files
inner join
master.dbo.sysfilegroups as groups
on
files.groupID = groups.groupID
where DBID =
    (select dbid from master.dbo.sysdatabases
            where [Name] = @databaseName)

-- Get the log file and its path
select @databaseLogFile = rtrim([Name]),
@databaseLogFilename = rtrim([Filename])
from master.dbo.sysaltfiles as files
where
    DBID = (select dbid from master.dbo.sysdatabases
                where [Name] = @databaseName)
    and groupID = 0

print 'Killing active connections to the "' + @databaseName + '" database'

-- Create the sql to kill the active database connections
set @execSql = ''
select @execSql = @execSql + 'kill ' + convert(char(10), spid) + ' '
from master.dbo.sysprocesses
where db_name(dbid) = @databaseName
and
DBID <> 0
and spid <> @@spid
exec (@execSql)

print 'Restoring "' + @databaseName + '" database from "' + @backupFileName + '" with '
print '  data file "' + @databaseDataFile + '" located at "' + @databaseDataFilename + '"'
print '  log file "' + @databaseLogFile + '" located at "' + @databaseLogFilename + '"'

set @execSql = '
restore database [' + @databaseName + ']
from disk = ''' + @backupFileName + '''
with
    file = 1,
    move ''' + @databaseDataFile + ''' to ' + '''' + @databaseDataFilename + ''',
    move ''' + @databaseLogFile + ''' to ' + '''' + @databaseLogFilename + ''',
    norewind,
    nounload,
    replace'

print @execSql
exec sp_executesql @execSql

exec('use ' + @databaseName)
go

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }Performance Analysis

http://www.sqlstuff.info/post/2007/10/What-indexes-are-used-most-often-(or-least-often)-on-my-server.aspx

Excel with SP’s

http://www.sqlservercentral.com/articles/Stored+Procedures/63537/


Leave a comment

Clone a Object in C#

 public static void Clone(this Object Source, Object Destination)
    {
        // exceptions
        if (Source == null)
            throw new ArgumentNullException("Source");
        if (Destination == null)
            throw new ArgumentNullException("Destination");
        if (Destination.GetType() != Source.GetType())
            throw new
        ArgumentException("Type mismatch");

        // copy each value over
        foreach (PropertyInfo pi in Source.GetType().GetProperties())
            if (pi.CanRead && pi.CanWrite)
                 pi.SetValue(Destination, pi.GetValue(Source, null), null);
    }