Browsing all articles in Code Snippets
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
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
{
}
}
{
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
{
}
}
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
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;
}
{
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;
}
Categories
- Dot Net (12)
- .Net Framework (1)
- Ajax (1)
- ASP.NET (6)
- C#.NET (3)
- Code Snippets (3)
- WCF (1)
- SQL Server (3)
- Tutorials (1)
- Video (1)




