Showing posts with label .net. Show all posts
Showing posts with label .net. Show all posts

Monday, January 2, 2023

Backup and Restore in C# Windows Form Application

Read Also:  


Programming SeekerzZ

To create backup of database from C# application and then restore it again from C# application follow the steps.
First design a form like bellow:
Form Design for Backup and Restore


Browse Button of Backup Option:

FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = dlg.SelectedPath;

            }


Backup Button Code:

string databse = con.Database.ToString();
            try
            {
                if (textBox1.Text == string.Empty)
                {
                    MessageBox.Show("Please Enter by Browseing Database Backup Location....!");
                }else
                {
                    string cmd = "BACKUP DATABASE [" + databse + "] TO DISK='" + textBox1.Text + "\\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";
                    using(SqlCommand command = new SqlCommand(cmd, con))
                    {
                        if(con.State != ConnectionState.Open)
                        {
                            con.Open();

                        }
                        command.ExecuteNonQuery();
                        con.Close();
                        MessageBox.Show("Databse Backup has been Successefully Created..!");

                    }
                }
            }catch(Exception ex)
            {
                MessageBox.Show("Error\n\n" + ex);

            }



Restore button Browse code:

OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "SQL SERVER database backup files|*.bak";
            dlg.Title = "Database restore";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dlg.FileName;

            }



Restore Button Code:

try
            {
                string database = con.Database.ToString();
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }
                try
                {
                    string sqlStmt2 = string.Format("ALTER DATABASE [" + database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
                    SqlCommand bu2 = new SqlCommand(sqlStmt2, con);
                    bu2.ExecuteNonQuery();

                    string sqlStmt3 = "USE MASTER RESTORE DATABASE [" + database + "] FROM DISK='" + textBox2.Text + "'WITH REPLACE;";
                    SqlCommand bu3 = new SqlCommand(sqlStmt3, con);
                    bu3.ExecuteNonQuery();

                    string sqlStmt4 = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
                    SqlCommand bu4 = new SqlCommand(sqlStmt4, con);
                    bu4.ExecuteNonQuery();

                    MessageBox.Show("Database Restoration Done Successefully");
                    con.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }catch(Exception ex) { }





If you are facing any problem in that program contact me with your issue :
https://www.facebook.com/usman.siddique.7771

Sunday, January 1, 2023

Thursday, November 10, 2022

How to delete data in C# from sql database : code in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

 
        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
            cnn.Open();
            string query = "delete from logout where name = '" +TextBox1.Text+ "';";
            SqlCommand cmd = new SqlCommand(query, cnn);
            cmd.ExecuteNonQuery();

            Response.Write("Deletion  is done!");

        }
    }
}

Wednesday, November 9, 2022

Insert data in SQL Database using c#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           
SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
cnn.Open();
           
string query="insert into logout(name,password) values('" + TextBox1.Text + "','" + TextBox2.Text + "' )";
SqlCommand cmd=new SqlCommand(query,cnn);
cmd.ExecuteNonQuery();

Response.Write("Saving is done!");


           
        }
         
        }
    }
}

Friday, November 27, 2015

Up-Date record in SQL Database using C#.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
            cnn.Open();
string query = "update logout set name ='"+TextBox3.Text+"' where name = '" + TextBox2.Text + "';";
            SqlCommand cmd = new SqlCommand(query, cnn);
            cmd.ExecuteNonQuery();

            Response.Write("update  is done!");

        }

   
    }
}


Friday, November 20, 2015

Retrieve / Collect data from SQL database using c#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

 
        protected void Button2_Click(object sender, EventArgs e)
        {

            try
            {
                SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
                cnn.Open();
                SqlCommand command = new SqlCommand("SELECT name,password from logout;", cnn);


                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    Response.Write("<table border=\"3\">");
                    while (reader.Read())
                    {
                        Response.Write("<tr><th width=\"40\">"+ reader.GetString(0) +"</th><th>"+ reader.GetString(1)+"</th></tr>");
                    } Response.Write("</table>");
                }
                else
                {
                    Response.Write("No rows found.");
                }
                reader.Close();
            }
            catch (SystemException ex) { Response.Write(ex); }
           
        }
    }
}


Saturday, October 24, 2015

Measurement Scales Converter in vb.net


Vb.net Code:-

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Single
        Dim b As Single

        Dim c As Single
        Dim inch As Single
        Dim yard As Single
        Dim meter As Single
        Dim fathom As Single
        Dim rod As Single
        Dim furlong As Single
        Dim kilometer As Single
        Dim mile As Single
        Dim foot As Single
        yard = 3
        meter = 3.28155
        fathom = 6
        rod = 16.5
        furlong = 660
        kilometer = 32815
        mile = 5280
        If Val(TextBox1.Text) = 1 Then
            a = inch
        End If
        If TextBox1.Text = 2 Then
            a = yard
        End If
        If TextBox1.Text = 3 Then
            a = meter
        End If
        If TextBox1.Text = 4 Then
            a = fathom
        End If
        If TextBox1.Text = 5 Then
            a = rod
        End If
        If TextBox1.Text = 6 Then
            a = furlong
        End If
        If TextBox1.Text = 7 Then
            a = kilometer
        End If
        If TextBox1.Text = 8 Then
            a = mile
        End If
        If Val(TextBox2.Text) = 1 Then
            b = inch
        End If
        If TextBox2.Text = 2 Then
            b = yard
        End If
        If TextBox2.Text = 3 Then
            b = meter
        End If
        If TextBox2.Text = 4 Then
            b = fathom
        End If
        If TextBox2.Text = 5 Then
            b = rod
        End If
        If TextBox2.Text = 6 Then
            b = furlong
        End If
        If TextBox2.Text = 7 Then
            b = kilometer
        End If
        If TextBox2.Text = 8 Then
            b = mile
        End If
        c = Val(TextBox3.Text) * a

        Dim ans As Integer

        ans = c / b
        TextBox4.Text = ans




    End Sub
End Class




Form:-



HTML CODE:-