希望明年能理所应当过个1024
使用ADO.NET技术开发数据库
功能实现代码
public void aa()
{
SqlConnection con;
con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
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();
}
在Page_load事件中调用aa方法,代码如下:
protected void Page_Load(object sender, EventArgs e)
{
this.aa();//调用aa方法
}
*“添加”*
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
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();//用哪个ExecuteNoneQuery()的方法来执行查询语句
con.Close(); //关闭数据库连接
}
*“删除”*
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from student where id=" + this.TextBox1.Text + "";
cmd.Connection = con;
con.Open();//打开数据库连接
cmd.ExecuteNonQuery();//用哪个ExecuteNoneQuery()的方法来执行查询语句
con.Close();//关闭数据库连接
}
*“修改”*
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con;
con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
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();//用哪个ExecuteNoneQuery()的方法来执行查询语句
con.Close();//关闭数据库连接
}
“查询”
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
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();
}
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");//用student表填充数据集
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
if (this.RadioButton3.Checked == true)
{
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from student where banji='" + this.TextBox3.Text + "'", con);
sda.Fill(ds, "student");//用student表填充数据集
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
刷新
```c
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=stu.mdf;User ID=sa;Password=123456");
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();
}
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");//用student表填充数据集
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
if (this.RadioButton3.Checked == true)
{
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from student where banji='" + this.TextBox3.Text + "'", con);
sda.Fill(ds, "student");//用student表填充数据集
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}