查询 删除 修改系统

来源:互联网 发布:手机切歌软件 编辑:程序博客网 时间:2024/04/30 00:40

前台代码

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" onclick="BtnAdd_Click" /></td>
            <td> <asp:Button ID="BtnSelect" runat="server" Text="查询" onclick="BtnSelect_Click"
                    style="height: 21px" /></td>
            <td>
                <asp:Button ID="btnUpdate" runat="server" Text="修改" onclick="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 WHEREStudentId=@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 whereStudentId=@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();
        }
    }
}

 

原创粉丝点击