数据库增改查

 <form id="form1" runat="server">
    学生编号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />
    <br />
    <br />
    学生编号:<asp:TextBox ID="txtstuid" runat="server"></asp:TextBox>
    <br />
    学生姓名:<asp:TextBox ID="txtstuname" runat="server"></asp:TextBox>
    <br />
    班级编号:<asp:TextBox ID="txtclaid" runat="server"></asp:TextBox>
    <br />
    班主任姓名:<asp:TextBox ID="txtbzrxm" runat="server"></asp:TextBox>
    <br />
    联系电话:<asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
    <br />
    家庭住址:<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button2" runat="server" Text="添加" onclick="Button2_Click" />
    <asp:Button ID="Button3" runat="server" Text="修改" onclick="Button3_Click" />
    </form>

---------------------后台部分-------------------------------

---------------------------------查询部分-----------------------------------

 string connstr = "data source=QZC-PC;DATABASE=News;INTEGRATED SECURITY=TRUE";
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT stuid,StuName,ClassId,BZUPhone,Mobile,Address FROM T_Students WHERE stuid=@Sid ";
            cmd.Parameters.AddWithValue("@Sid",TextBox1.Text.Trim());
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);
           
            txtstuid.Text = dt.Rows[0]["stuid"].ToString();
            txtstuname.Text = dt.Rows[0]["StuName"].ToString();
            txtclaid.Text = dt.Rows[0]["ClassId"].ToString();
            txtbzrxm.Text = dt.Rows[0]["BZUPhone"].ToString();
            txtphone.Text = dt.Rows[0]["Mobile"].ToString();
            txtaddress.Text = dt.Rows[0]["Address"].ToString();
        }

-=-----------------------------插入部分---------------------------------------------

        protected void Button2_Click(object sender, EventArgs e)
        {
             SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "insert into T_Students(stuid,StuName,ClassId,BZUPhone,Mobile,Address)values(NEWID(),@StuName,@ClassId,@BZUPhone,@Mobile,@Address)";
            cmd.Parameters.AddWithValue("@StuName",txtstuname.Text.Trim());
            cmd.Parameters.AddWithValue("@ClassId", txtclaid.Text.Trim());
            cmd.Parameters.AddWithValue("@BZUPhone",txtbzrxm.Text.Trim());
            cmd.Parameters.AddWithValue("@Mobile",txtphone.Text.Trim());
            cmd.Parameters.AddWithValue("@Address",txtaddress.Text.Trim());
            int n=cmd.ExecuteNonQuery();
            if(n>0)
            {
                Response.Write("插入成功!");
            }
        }

--------------------------修改部分---------------------------------------------------------

        protected void Button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "update T_Students set ClassId=@ClassId,BZUPhone=@BZUPhone,Mobile=@Mobile,Address=@Address,StuName=@StuName where stuid=@stuid";
            cmd.Parameters.AddWithValue("@ClassId",txtclaid.Text.Trim());
            cmd.Parameters.AddWithValue("@BZUPhone",txtbzrxm.Text.Trim());
            cmd.Parameters.AddWithValue("@Mobile",txtphone.Text.Trim());
            cmd.Parameters.AddWithValue("@Address",txtaddress.Text.Trim());
            cmd.Parameters.AddWithValue("@StuName",txtstuname.Text.Trim());
            cmd.Parameters.AddWithValue("@stuid",txtstuid.Text.Trim());
            int n=cmd.ExecuteNonQuery();
            if(n>0)
            {
                Response.Write("修改成功!");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值