使用非类型化的DataSet完成用户信息的增删改查操作

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

 

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button4" runat="server" Text="查找" οnclick="Button4_Click" />

        <asp:Button ID="Button1" runat="server" Text="更改" οnclick="Button1_Click" />


        <asp:Button ID="Button3" runat="server" Text="删除" οnclick="Button3_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
      
       
        name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
      price:  <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
       photoPath:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
 
<asp:Button ID="Button2" runat="server" Text="添加" οnclick="Button2_Click" />
 <br />


        <br />

 


        <br />
   

 


    </div>
    </form>
</body>
</html>

这是HTML页面的代码

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strcnn))
        {
            SqlCommand sqlCmm = sqlCnn.CreateCommand();
            sqlCmm.CommandText = "Select * from pros";
            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
            sqlCnn.Open();
            sqlDA.Fill(ds);
            ds.Tables[0].Rows[0]["name"] = TextBox1.Text;
            SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
            sqlDA.Update(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
  
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds1 = new DataSet();
        string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strcnn))
        {
            SqlCommand sqlCmm = sqlCnn.CreateCommand();
            sqlCmm.CommandText = "Select * from pros";
            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
            sqlCnn.Open();
            sqlDA.Fill(ds1);
            DataRow dr = ds1.Tables[0].NewRow();
            dr["name"] = TextBox2.Text;
            dr["price"] = TextBox3.Text;
            dr["photoPath"] = TextBox4.Text;
            ds1.Tables[0].Rows.Add(dr);
            SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
            sqlDA.Update(ds1);
            this.GridView1.DataSource = ds1;
            this.GridView1.DataBind();
        }
     
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
         DataSet ds2 = new DataSet();
        string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strcnn))
        {
            SqlCommand sqlCmm = sqlCnn.CreateCommand();
            sqlCmm.CommandText = "Select * from pros";
            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
            sqlCnn.Open();
            sqlDA.Fill(ds2);
            DataRow[] row = ds2.Tables[0].Select("name='"+TextBox1.Text+"'");
            foreach (var item in row)
            {
                item.Delete();
            }

          


            SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
            sqlDA.Update(ds2);
            this.GridView1.DataSource = ds2;
        this.GridView1.DataBind();
        }
      
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        DataSet ds3 = new DataSet();
        string strcnn = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strcnn))
        {
            SqlCommand sqlCmm = sqlCnn.CreateCommand();
            sqlCmm.CommandText = "Select * from pros where name='"+TextBox1.Text+"'";
            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmm);
            Response.Write(sqlDA.ToString());
            sqlCnn.Open();
            sqlDA.Fill(ds3);
            SqlCommandBuilder builder = new SqlCommandBuilder(sqlDA);
            sqlDA.Update(ds3);
            this.GridView1.DataSource = ds3;
            this.GridView1.DataBind();
        }
    }

}

这是.aspx的代码 能够简实现增删改查

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值