C#.Net中ADO.net数据增删查改操作

本文介绍如何在ASP.NET环境中利用ADO.NET进行SQL数据库的查询、添加、删除和修改操作。通过GridView控件展示数据表,并实现基于TextBox输入的查询和按钮触发的数据插入功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在ASP.Net窗口中,进行数据库的查询,添加,删除,修改操作

在网页中使用GridView进行数据库表单的显示,以及基本的数据库操作,SqlCommand和SqlDataAdapter

查询功能,插入数据功能:(在Textbox中输入内容,点击按钮执行查询插入功能)

 protected void Button1_Click(object sender, EventArgs e)    //查询功能
        {
            if (this.TextBox1.Text != "")       //判断输入框是否为空
            {
                //与数据库进行连接
                SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456");
                myConn.Open();
                //查询操作
                string sqlStr = "select * from UserTable where UserName=@UserName";
                SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
                myCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = this.TextBox1.Text.Trim();
                SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
                //将查询的结果添加到DataSet中
                DataSet myDs = new DataSet();
                myDa.Fill(myDs);
                //在GridView中显示查询到的结果
                if (myDs.Tables[0].Rows.Count > 0)
                {
                    GridView1.DataSource = myDs;
                    GridView1.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('查询结果为空')</script>");
                }
                myDa.Dispose();
                myDs.Dispose();
                myConn.Close();
            }
            else
            {
                Response.Write("
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值