1. 程式人生 > >數據庫常用操作

數據庫常用操作

打開 bin 打開數據庫 web n) checked default nta dataset

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();//打開數據庫連接
DataSet ds = new DataSet();
sda.Fill(ds, "student");//用student表填充數據集
con.Close();//關閉數據庫連接
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.ConnectionString = "server=(local);uid=sa;pwd=;database=stu;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into student values(" + this.TextBox1.Text + "," + this.TextBox2.Text + "," + this.TextBox3.Text + ")";//添加學生信息
cmd.Connection = con;
con.Open(); //打開數據庫連接
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法來執行查詢語句
con.Close(); //關閉數據庫的連接
}

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 + "";
//根據學生的編號來修改相應的學生的信息
cmd.Connection = con;
con.Open();//打開數據庫的連接
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法來執行查詢語句
con.Close();//關閉數據庫的連接

}
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 + "";
//根據學生的編號來刪除相應的學生的信息
cmd.Connection = con;
con.Open();//打開連接
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法來執行查詢語句
con.Close();//關閉連接

}
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";// 查詢student表
cmd.Connection = con;
con.Open();//打開數據庫的連接
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cmd.ExecuteNonQuery();//用ExecuteNonQuery()的方法來執行查詢語句
DataSet ds = new DataSet();
sda.Fill(ds, "student");//填充數據集
con.Close();//關閉數據庫的連接
this.GridView1.DataSource = ds;//輸出到GridView控件中
this.GridView1.DataBind();//將數據綁定到GridView控件中

}
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=stu");
con.Open();//打開數據庫的連接
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");//把student表填充到數據集中
this.GridView1.DataSource = ds;
this.GridView1.DataBind();//將數據綁定到GridView控件中
}
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();//將數據綁定到GridView控件中
}
}

數據庫常用操作