Browsing all articles in Code Snippets
Sep
8

How to take backup all databases in SQL Server

The following code will helps you to take all the databases at a time

DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\SQLBackup\'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
 
 
 
 
Apr
6

Code Snippet : Exporting DataSet to Excel file in asp.net

Here is the code to export DataSet to Excel file.
Process:
Prepare a GridView or DataGrid from code behind file with the DataSet
and use the following code to export as a Excel(.xls) file.

private void Export(DataSet ds)
{
System.Web.UI.WebControls.DataGrid dg;
try
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=ExportFile.xls");
EnableViewState = true;
foreach (DataTable item in ds.Tables)
{
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
dg = new System.Web.UI.WebControls.DataGrid();
dg.ShowHeader = true;
dg.HeaderStyle.BackColor = System.Drawing.Color.Gray;
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.ForeColor = System.Drawing.Color.White;
dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dg.HeaderStyle.Height = new Unit(30);
dg.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
dg.ForeColor = System.Drawing.Color.Black;
dg.DataSource = item;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(item.TableName);
Response.Write(stringWrite.ToString());
}
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
}
}
Jan
28

Code Snippet 1: Best way to convert string to DateTime

This is the best way to convert string to Date
the following code convert string to date which is in
“d/M/yyyy” or “dd/MM/yyyy” or “d/MM/yyyy” or “dd/M/yyyy”

string MyDate = "12/12/2012";
DateTime _MyDate;
if (!DateTime.TryParseExact(MyDate, new[] { "d/M/yyyy", "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out _MyDate))
{
this._message = "Invalid Date";
// do here with invalid date
}
_MyDate
// do here  with valid date

Function:

public static bool ToDate(string DateString, out DateTime Date)
        {
            if (!DateTime.TryParseExact(DateString, new[] { "d/M/yyyy", "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out Date))
                return false;
            return true;
        }
Follow us on Twitter! Follow us on Twitter!
FoxSparrow Tweets

Categories