ASP.NET

这篇博客展示了如何使用ADO.NET进行数据库操作,包括读取、添加、删除和更新记录。通过SqlConnection、SqlCommand和SqlDataAdapter等组件,实现了与SQL Server数据库的交互。示例代码详细地解释了如何在ASP.NET环境中执行SQL命令,以及如何处理用户输入来完成CRUD操作。
摘要由CSDN通过智能技术生成

希望明年能理所应当过个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();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值