Monday, October 21, 2013

How to determine list of all the databases and size of the database ?

 

Dear All,

Sometimes you require list of all the database and size.

To achieve this we need to write  following script

 

   1:  
   2: SELECT sd.name,Convert(Decimal(10,0),(mf.size /1024)) As size
   3: FROM 
   4: sysdatabases sd
   5: INNER JOIN sys.master_files mf ON mf.database_id = 
   6: sd.dbid
   7:  
   8: AND mf.type = 0 
   9: WHERE sd.Name Not IN ('MASTER','tempdb','Model','MSDB')

 



Hope it will resolve your query. Enjoy :)


Rajat Jaiswal

Monday, October 14, 2013

How to do 7zip programmatically using C# ?

Dear All,

Some times it may be possible you require to  make zip of  a file or directory.

so here is simple example.

To start it you have to download “SevenZipSharp.dll”, “7z.dll” you can download sdk from http://www.7-zip.org/sdk.html

Once you download sdk copy above 2 DLL in your project bin folder.

Now write below code which i am writing here

   1:  
   2: public bool CompressFolder(String 
   3: backupFolder)
   4:  
   5: {
   6:  
   7: try
   8:  
   9: {
  10:  
  11: SevenZipCompressor sz = new SevenZipCompressor(); 
  12:  
  13: SevenZip.SevenZipCompressor.SetLibraryPath(DLLPath);
  14:  
  15: sz.CompressionLevel = 
  16: CompressionLevel.Ultra;
  17:  
  18: sz.CompressionMode = 
  19: CompressionMode.Create;
  20:  
  21: sz.CompressionMethod = 
  22: CompressionMethod.Default;
  23:  
  24: sz.FastCompression = 
  25: true;
  26:  
  27: sz.CompressDirectory(backupFolder, BackupPath + "\\" + 
  28: DateTime.Now.Date.ToString("yyyyMMddHHmmss") + 
  29: "_7Zip.7z");
  30:  
  31: Directory.Delete(backupFolder,true);
  32:  
  33: return true;
  34:  
  35: }
  36:  catch (Exception 
  37: ex)
  38:  
  39: {
  40:  
  41: throw ex;
  42:  }



Now let me describe the code to you.


This is function which compress the folder to 7zip what ever pass as a paramter


in this function we have created a sevenzip compressor object


SevenZip.SevenZipCompressor.SetLibraryPath(DLLPath);


in the above line we set the DLLpath which is basically your bin folder path


sz.CompressionLevel = CompressionLevel.Ultra; is compress level which can be different according to your need.


sz.CompressDirectory(backupFolder, BackupPath + "\\" + DateTime.Now.Date.ToString("yyyyMMddHHmmss") + "_7Zip.7z");


above row compress the directory of backupfolder and rename with datetime_7zip.7z


I hope this will help you.


Enjoy 7zip.


Thanks & Best Regards,


Rajat Jaiswla