Duminda Chamara

Wednesday, June 17, 2009

MS SQL Database database backup

How to Create a Backup in SQL Server 2005?


This is the query for create a SQL server backup


BACKUP DATABASE [data_base_name] --Name of the database
TO DISK = N'F:\Backups\BackOffic.bak' --Location to save backup file
WITH NOFORMAT, --Specifies that the backup operation preserves the existing media header and backup sets on the media volumes used for this backup operation
INIT, --Specifies that all backup sets should be overwritten
NAME = N'Mye-Full Database Backup'
, --Name of the backup file
SKIP, --Disables the checking of backup set expiration and name that is usually performed by the BACKUP statement to prevent overwrites of backup sets.
NOREWIND, --Specifies that SQL Server will keep the tape open after the backup operation
NOUNLOAD, --Specifies that after the BACKUP operation the tape will remain loaded on the tape drive.
STATS = 10
--Displays a message each time another percentage completes (displays a message after each 10 percent is completed.)

Friday, June 12, 2009

Sending an email in ASP.Net using C#


C# Code for sending  an email in ASP.Net


You can use following code to send an email in asp.net using C#

public bool SendMail()
{
try
{
string SERVER = txtSMTP.Text;

MailMessage objMsg = new MailMessage();
int port = 25;//SMTP port number
objMsg.BodyFormat = MailFormat.Text;
objMsg.To = "abc@gmail.com";

objMsg.From = "yourmail@yourdomain";
objMsg.Cc = "xyz@gmail.com";
objMsg.Subject = "Sending E-Mails using C#";

string userName = "info@abc.com";
string password = "123";
int cdoBasic = 1;
int cdoSendUsingPort = 2;

objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", SERVER);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
objMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);

objMsg.Body = "This is the message body";

//If mail has an attatchment
if (FileUpload1.HasFile)
{

string filename = Server.MapPath("") + "\\userfiles\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(filename);

objMsg.Attachments.Add(new MailAttachment(filename));
}

SmtpMail.SmtpServer = SERVER;

SmtpMail.Send(objMsg);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ShowMessage("Mail Sending Fail");
return false;
}
}