Wednesday, 25 June 2014

Check wheather the tabel is exist or not in database and if not then create

public void check()           //check table exist or not
    {
            string x = TextBox1.Text;
            int i = 0;
            con.Open();
            string s = "SELECT * FROM "+x+" WHERE 1 = 0";
            SqlCommand cmd = new SqlCommand(s, con);
            cmd.ExecuteNonQuery();
            con.Close();
            i = 1;
            if (i == 1)
            {
                Response.write("table doesn't exist");
            }
            else
            {
                Response.Redirect("Default2.aspx");
            }
    }


// NOW CREATE A TABLE


        string x = TextBox1.Text;
        con.Open();
        string s = "create table " + x + " (" + "name nvarchar(50)," + "gender nvarchar(50)," + "email nvarchar(50)," + "mobile nvarchar(50))";
        SqlCommand cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        Response.Redirect("Default.aspx");
    }

No comments:

Post a Comment