Showing posts with label C#. Show all posts
Showing posts with label C#. 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!");


           
        }
         
        }
    }
}

Thursday, August 4, 2016

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); }
           
        }
    }
}


Monday, October 5, 2015

Polymorphism in C# : Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Shape
    {
        protected int width, height;
        public Shape()
        {
        }
        public virtual void area(int width, int height)
        {
            Console.WriteLine("Parent class area :");
            Console.WriteLine("Shape has no spasific area ");
        }
    }

    class Rectangle : Shape
    {
        public Rectangle() : base()
        {

        }
        public override void area(int width, int height)
        {
            Console.WriteLine("Rectangle class area :");
            Console.WriteLine(width * height);
        }
    }
   
    class Triangle : Shape
    {
        public Triangle() : base()
        {

        }
        public new void area(int width,int height)
        {
            Console.WriteLine("Triangle class area :");
            Console.WriteLine(width * height / 2);
        }
    }
   
    class Tester
    {

        static void Main(string[] args)
        {
            Shape s = new Shape();
            Rectangle r = new Rectangle();
            Triangle t = new Triangle();
            s.area(0,0);
            r.area(10,7);
            t.area(10,5);

            Shape s1 = new Rectangle();
            s1.area(10, 15);

            Shape s2 = new Triangle();
            s2.area(22, 76);

            Console.ReadKey();
        }
    }
}

Sunday, September 13, 2015

Interfaces in C#

An interface is defined as a syntactical contract that all the classes inheriting the interface should follow. The interface defines the 'what'part of the syntactical contract and the deriving classes define the 'how'part of the syntactical contract.
Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.
Abstract classes to some extent serve the same purpose, however, they are mostly used when only few methods are to be declared by the base class and the deriving class implements the functionalities.



using System;
namespace ArrayApplication
{
    public interface person{
         void name(string n);
         void age(int a );
         void clas(int c);
    }
   
    public class usman : person{
    public  void name(string n){
        Console.WriteLine("Usman name= "+n);
    }
    public void age(int a){
        Console.WriteLine("Usman age= "+a);
    }
    public void clas(int c){
        Console.WriteLine("Usman class= "+c);
    }
    }
   
    public class usama : person{
    public void name(string n){
        Console.WriteLine("Usman name= "+n);
    }
    public void age(int a){
        Console.WriteLine("Usman class= "+a);
    }
    public void clas(int c){
        Console.WriteLine("Usman class= "+c);
    }  
    }
   
   public class test
   {
       public static void Main(String[] args){
           usama obj=new usama();
           obj.name("Usama hussain");
           obj.age(20);
           obj.clas(10);
         
           usman obj1=new usman();
           obj1.name("Usman Siddique");
           obj1.age(21);
           obj1.clas(09);
       }
   }
}

=========================================
using System;
namespace ArrayApplication
{
    public interface abc{
         void fun();
              }

   public class MyArray : abc
   {
                                                         public void fun(){
                                                        Console.WriteLine("this is defination of interface fun");
                                                       }
                                                       static void Main(string[] args){
                                                       MyArray obj=new MyArray();
                                                      obj.fun();
                                                      }    }    }
       
   

Inheritance in C#

using System;
namespace ArrayApplication
{
    class abc{
        public void fun(){
            Console.WriteLine("This is A function of base class abc");
        }
    }
   class MyArray : abc  
   {
       static void Main(string[] args)
      {
         MyArray obj=new MyArray();
         obj.fun();
      }
   }
}

Argument / Parameter passing in C# Functions

using System;
namespace ArrayApplication
{
   class MyArray
   {
       public MyArray(){
           Console.WriteLine("Construtor Invoke");
       }
       public void myfun(int a){
           Console.WriteLine("User Pass the value = "+a);
       }
      static void Main(string[] args)
      {
         MyArray obj=new MyArray();
         obj.myfun(20);
       
      }
   }
}

Functions in C#

using System;
namespace ArrayApplication
{
   class MyArray
   {
       public MyArray(){
           Console.WriteLine("Construtor Invoke");
       }
       public void myfun(){
           Console.WriteLine("Its a funtion under My Array class");
       }
      static void Main(string[] args)
      {
         MyArray obj=new MyArray();
         obj.myfun();
       
      }
   }
}

Object Creation and Constructor in C#

using System;
namespace ArrayApplication
{
   class MyArray
   {
       public MyArray(){
           Console.WriteLine("Construtor Invoke");
       }
      static void Main(string[] args)
      {
         MyArray obj=new MyArray();
       
      }
   }
}

Create a table in C#

using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int no,i;
         no=5;
         for(i=1;i<=10;i++){
             Console.WriteLine(no+" * "+i+" = "+no*i);
         }
      }
   }
}

Hello World in C#

using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Helo World...Its my First C# Program");
      }
   }
}