查询 删除 修改系统

前台代码

WebForm.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="查询_增删.WebForm2" %>

<!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>
     <table>
     <tr><td>学生编号</td>
     <td>
         <asp:TextBox ID="txtStudentId" runat="server"></asp:TextBox></td></tr>

     <tr>
          <td>学生姓名 </td><td>
         <asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
         </td></tr>
         <tr><td>班级编号</td><td>
             <asp:TextBox ID="txtClassId" runat="server"></asp:TextBox>
             </td></tr>
         <tr><td>班主任姓名</td><td>
             <asp:TextBox ID="txtBzrName" runat="server"></asp:TextBox>
             </td></tr>
        <tr><td>联系电话</td><td>
            <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
            </td></tr>
        <tr><td>家庭住址</td><td>
            <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
            </td></tr>
        <tr>
            <td><asp:Button runat="server" Text="添加" ID="BtnAdd" οnclick="BtnAdd_Click" /></td>
            <td> <asp:Button ID="BtnSelect" runat="server" Text="查询" οnclick="BtnSelect_Click"
                    style="height: 21px" /></td>
            <td>
                <asp:Button ID="btnUpdate" runat="server" Text="修改" οnclick="btnUpdate_Click"
                    style="height: 21px" />
                </td>
            </tr>
     </table>
    </div>
    </form>
</body>
</html>

 

后台代码

WebForm.aspx.cs

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

namespace 查询_增删
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        string strcon = @"Data Source=PC-20121114LMOU;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=842674";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //插入
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO T_Students(StudentId,StudentName,ClassId,BzrName,Photo,Address) VALUES (NEWID(),@txtStudentName,@txtclassid,@txtbzrname,@txtphoto,@txtaddress)";
            cmd.Parameters.AddWithValue("@txtStudentName", txtStudentName.Text);
            cmd.Parameters.AddWithValue("@txtclassid", txtClassId.Text);
            cmd.Parameters.AddWithValue("@txtbzrname", txtBzrName.Text);
            cmd.Parameters.AddWithValue("@txtphoto", txtPhone.Text);
            cmd.Parameters.AddWithValue("@txtaddress", txtAddress.Text);

            cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();


           

        }
        //查询
        protected void BtnSelect_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT StudentId,StudentName,ClassId,BzrName,Photo,Address FROM T_Students WHERE StudentId=@txtStudentId";
            cmd.Parameters.Add(new SqlParameter("@txtStudentId", txtStudentId.Text.Trim()));
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            conn.Dispose();
            //Response.Write(dt.Rows.Count);
            txtStudentId.Text = Convert.ToString(dt.Rows[0]["StudentId"]);
            txtStudentName.Text = Convert.ToString(dt.Rows[0]["StudentName"]);
            txtClassId.Text = Convert.ToString(dt.Rows[0]["ClassId"]);
            txtClassId.Text = Convert.ToString(dt.Rows[0]["ClassId"]);
            txtBzrName.Text = Convert.ToString(dt.Rows[0]["BzrName"]);
            txtPhone.Text = Convert.ToString(dt.Rows[0]["Photo"]);
            txtAddress.Text = Convert.ToString(dt.Rows[0]["Address"]);

 

        }

        //修改
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string studentname = txtStudentName.Text;
            string classid = txtClassId.Text;
            string bzrname = txtBzrName.Text;
            string photo = txtPhone.Text;
            string address = txtAddress.Text;

            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "UPDATE T_Students SET StudentName=@txtStudentName,ClassId=@txtclassid,BzrName=@txtbzrname,Photo=@txtphoto,Address=@txtaddress where StudentId=@txtstudentid ";
            cmd.Parameters.Add(new SqlParameter("@txtStudentName", txtStudentName.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@txtStudentId", txtStudentId.Text.Trim()));

            cmd.Parameters.Add(new SqlParameter("@txtclassid", txtClassId.Text));
            cmd.Parameters.Add(new SqlParameter("@txtbzrname", txtBzrName.Text));
            cmd.Parameters.Add(new SqlParameter("@txtphoto", txtPhone.Text));
            cmd.Parameters.Add(new SqlParameter("@txtaddress", txtAddress.Text));

            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Write("更改成功");
            }
            cmd.Dispose();
            conn.Dispose();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值