Friday, 21 October 2016

Shrink SQL server database log

Database log increases with increasing transactions and database activity. At times it is not required to maintain the log after some period. So in such cases it becomes quite beneficial to shrink the database log file as it saves much disk space. Below query can be used to do the needful.


Output:



sometimes it is better to set the database to simple recovery before compressing log.

Consider below query in those cases:
use @databaseName;
Go 
ALTER DATABASE @databaseName SET RECOVERY SIMPLE
DBCC shrinkfile (@logFileName, 8);
ALTER DATABASE @databaseName SET RECOVERY FULL

Monday, 17 October 2016

Sending Email via SMTP client


Simple Mail Transfer Protocol commonly known as SMTP is the protocol for sending and receiving email. .NET allows to use this SMTP service via its SMTP client.

Consider the below code:

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
                            smtpClient.UseDefaultCredentials = false;
                            smtpClient.Credentials = new System.Net.NetworkCredential("abc@live.com", "hr1?");
                            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                            smtpClient.EnableSsl = true;
                            smtpClient.Timeout = 20000;
                            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                            
//Setting From , To and CC
                            mail.From = new System.Net.Mail.MailAddress("abc@live.com", "Game");
                            mail.To.Add(new System.Net.Mail.MailAddress("efg@live.com"));
                            smtpClient.Send(mail);


  • In the above code first an object of smptClient is created. 
  • Host server name (here smtp.gmail.com) and port number (here 587)  is provided as parameter.
  • We do not want to use default credentials here so just set the respective attribute of smtpClient to false.
  • Here we use the email "abc@live.com" with password "hr1?" as the sender credentials.
  • Delivery method of SMTP is set to Network so that mail is send via network.
  • EnableSsl is set true to make the transmission secure by SSL (secure socket layer)
  • Setting timeout of mail process to 20000 milliseconds (20 seconds)
  • Mail message object "mail" is instantiated.
  • Mail from (here abc@live.com with name "Game") and mail to (here efg@live.com) is set
  • smtpClient sends the provided mail with Send() function


Monday, 3 October 2016

Retrieve Comma Separated Values



Some times there are certain scenarios where data needs to be gathered as a collection. For instance multiple records need to be set as single record, in a such cases setting row data as comma separated column value can be quite effective.

SQL server allows to incorporate this with its built-in feature of stuff allowing row data to be merged as a single column value.

Code:
select  
(STUFF((SELECT ',' + CAST(a.username AS VARCHAR(10))+''' ' [text()]
FROM [users] a
FOR XML PATH(''), TYPE)
.value('.','NVARCHAR(MAX)'),1,2,' '))
as AllNames 

Output: