Database common operations

Source: Internet
Author: User

Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
System.Web.UI;
Using System.Web.UI.WebControls;
public partial class _default:system.web.ui.page
{
public void filling ()
{
SqlConnection con = new SqlConnection ();
Con. ConnectionString = "server= (local); uid=sa;pwd=sa;database=stu;";
SqlCommand cmd = new SqlCommand ();
Cmd.commandtext = "SELECT * from student";
Cmd. Connection = con;
SqlDataAdapter SDA = new SqlDataAdapter ();
Sda. SelectCommand = cmd;
Con. Open ();//Opening a database connection
DataSet ds = new DataSet ();
Sda. Fill (ds, "Student");//populate Data set with student table
Con. Close ();//Closing database connection
This. Gridview1.datasource = ds;
This. Gridview1.databind ();
}

Protected void Page_Load (object sender, EventArgs e)
{
this.filling ();
}
protected void Button1_Click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ();
Con.c onnectionstring = "server= (local); uid=sa;pwd=;d atabase=stu;";
SqlCommand cmd = new SqlCommand ();
Cmd.commandtext = "INSERT into student values (" + this. TextBox1.Text + "," + this. TextBox2.Text + "," + this. TextBox3.Text + ")";//Add student information
cmd. Connection = con;
Con. Open (); Open the database connection
CMD. ExecuteNonQuery ();//Use ExecuteNonQuery () method to execute the query statement
Con. Close (); Close the connection to the database
}

protected void Button3_Click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ();
Con. ConnectionString = "server= (local); uid=sa;pwd=sa;database=stu;";
SqlCommand cmd = new SqlCommand ();
Cmd.commandtext = "Update student set Name= '" + this. TextBox2.Text + "', banji= '" + this. TextBox3.Text + "' where id=" + this. TextBox1.Text + "";
//Modify the corresponding student's information according to the student's number
cmd. Connection = con;
Con. Open ();//Opens the connection to the database
cmd. ExecuteNonQuery ();//Use ExecuteNonQuery () method to execute the query statement
Con. Close ();//Closes the connection to the database

}
protected void button2_click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ();
Con. ConnectionString = "server= (local); uid=sa;pwd=sa;database=stu;";
SqlCommand cmd = new SqlCommand ();
Cmd.commandtext = "Delete from student where id=" + this. TextBox1.Text + "";
//delete the corresponding student's information according to the student's number
cmd. Connection = con;
Con. Open ();//Opens Connection
cmd. ExecuteNonQuery ();//Use ExecuteNonQuery () method to execute the query statement
Con. Close ();//Closes connection

}
protected void Button4_Click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ();
Con. ConnectionString = "server= (local); uid=sa;pwd=sa;database=stu;";
SqlCommand cmd = new SqlCommand ();
Cmd.commandtext = "SELECT * from student";//Query student table
cmd. Connection = con;
Con. Open ();//Opens the connection to the database
SqlDataAdapter SDA = new SqlDataAdapter ();
Sda. SelectCommand = cmd;
Cmd. ExecuteNonQuery ();//Use the ExecuteNonQuery () method to execute the query statement
DataSet ds = new DataSet ();
Sda. Fill (ds, "Student");//populate data set
Con. Close ();//Closes the database connection
this. Gridview1.datasource = ds;//output to the GridView control in
this. Gridview1.databind ();//Bind data to the GridView control

}
protected void Button5_click (object sender, EventArgs e)
{
SqlConnection con = new SqlConnection ("server= (local); Uid=sa;pwd=sa;database=stu");
Con. Open ();//Opening a connection to a database
if (this. radiobutton1.checked = = True)
{
SqlCommand cmd = new SqlCommand ();
DataSet ds = new DataSet ();
SqlDataAdapter SDA = new SqlDataAdapter ("select * from student where id="
+ this. TextBox1.Text + "", con);
Sda. Fill (ds, "Student");//populate the student table with the data set
This. Gridview1.datasource = ds;
This. Gridview1.databind ();//Bind data to the GridView control
}
if (this. radiobutton2.checked = = True)
{
SqlCommand cmd = new SqlCommand ();
DataSet ds = new DataSet ();
SqlDataAdapter SDA = new SqlDataAdapter ("select * from student where name= '"
+ this. TextBox2.Text + "'", con);
Sda. Fill (ds, "Student");
This. Gridview1.datasource = ds;
This. Gridview1.databind ();//Bind data to the GridView control
}
}

Database common operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.