Showing posts with label Data base. Show all posts
Showing posts with label Data base. 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

Saturday, December 31, 2022

How to search and find data from SQL database using PHP

In last post we learn how to delete data from sql database in this post i will show you how to find some data and verify that data from sql database using php.

So for this SQL select query is used and we select this time a user name and password to verify them. If the user name match with the name in database then this show an output that user verified.



<?php
include "conn.php";

$user = $_POST['user'];
$pass = $_POST['password'];


$sql = "select user,password from alogin where user='".$user."' and password='".$pass."';";
$result=$conn->query($sql);


if ($result->num_rows > 0) {
    echo "User Athenticate successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>


Create-connection-with-sql-database-in-php

Saturday, December 10, 2022

Up-Date data in SQL database using Php

In last two posts i discus how to create connection and how to insert data in data base using php.

Insert-data-in-SQL-database-using-PHP


Create-connection-with-sql-database-in-php

In this post i will show you how to update data in SQL database table using php.
so for this first we need database connection which we create in first post so just include that file and then code for update query


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!");


           
        }
         
        }
    }
}

Sunday, April 24, 2016

Delete Data in SQL Database using PHP

In my last three posts i covers the topics that how to create connection insert data and then if we want to update that data so then how to update data in in sql database table using php.

For step by step learning see my privies posts on database and php.









Today in this post we delete a record from login table we take a form inputs from user where he gave us the name and password of the user which he want to delete.


<?php
include "conn.php";

$user = $_POST['user'];
$pass = $_POST['password'];


$sql = "DELETE from login 
        where user='".$user."' and PASSWORD='".$pass."' ";

if ($conn->query($sql) === TRUE) {
    echo "Record Deleted successfully";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>




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


Sunday, November 2, 2014

Create Foreign key in Sql

How To create Foreign key in sql table :

You have a code(for table)like this:

create table person( name     varchar2(5),
                               Id_no       number(3),
                              Address    number(10) );

and you have another table of place:

create table place(name   varchar2(5)primary key,
                             location  varchar2(5),
                             owner_name  varchar2(5) );

Now you want to make owner_name  as Foreign key which relate name of person to the owner name of place table. so there are two methods to make a column foreign key one in table order and other one is column order.
===================================================
Column  Order:
create table place(name              varchar2(5)    primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5)   references   person(name) );
==================================================
Table Order:
create table place(name              varchar2(5)   primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5),
                            constraint   n_pk    foreign key (owner_name)  references   person(name) );

Note:All the bold words are reserved word in Sql.

See Also :How to create Primary Key in data base table using Sql Command.


Create Primary Key in Sql table

How To create primary key in sql table :

You have a code(for table)like this:

create table person( name     varchar2(5),
                               Id_no       number(3),
                              Address    number(10));

Now you want to make Id_no as Primary key: so there are two methods to make a column primary key one in table order and other one is column order.
===================================================
Column  Order:
create table person( name    varchar2(5),
                               Id_no     number(3) primary key,
                              Address  number(10));
==================================================
Table Order:
create table person( name varchar2(5),
                               Id_no number(3),
                              Address number(10),
                             primary key(Id_no));

Create Table in Sql

How to create table in sql:-

The create table statement is used to create a new table.
Example:
create table employee
(first varchar(15),
 last varchar(20),
 age number(3),
 address varchar(30),
 city varchar(20),
 state varchar(20));
 
To create a new table, enter the keywords create table followed by the table name, followed by an open parenthesis, followed by the first column name, followed by the data type for that column, followed by any optional constraints, and followed by a closing parenthesis. It is important to make sure you use an open parenthesis before the beginning table, and a closing parenthesis after the end of the last column definition. Make sure you seperate each column definition with a comma. All SQL statements should end with a ";".

Here are the most common Data types:
char(size)Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
varchar(size)Variable-length character string. Max size is specified in parenthesis.
number(size)Number value with a max number of column digits specified in parenthesis.
dateDate value
number(size,d)Number value with a maximum number of digits of "size" total, with a maximum number of "d" digits to the right of the decimal.


See Also:   How To Create Primary Key in Sql table


See Also:  How to create Foreign Key Using Sql Command