ADO--小练习

本博客介绍如何使用ASP.NET创建添加学生信息的窗体,并通过SQL Server数据库实现学生信息的存储,包括输入学生的基本信息如班级代号、姓名、年龄、性别、出生年月、手机号和身份证号,以及自动获取新增学生ID的功能。
摘要由CSDN通过智能技术生成
练习:添加学生窗体。返回最新添加的学生的自动编号Id
前台
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        请输入学生的字段值:<br />
        班级代号:<asp:TextBox ID="txtclassid" runat="server"></asp:TextBox>
        <br />
        姓名:<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
        <br />
        年龄:<asp:TextBox ID="txtage" runat="server"></asp:TextBox>
        <br />
        性别:<asp:TextBox ID="txtgender" runat="server"></asp:TextBox>
        <br />
        出生年月:<asp:TextBox ID="txtbirthary" runat="server"></asp:TextBox>
        <br />
        手机号:<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
        <br />
        身份证号:<asp:TextBox ID="txtCardid" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btncharu" runat="server" οnclick="btncharu_Click"
            Text="Button" />
        <br />
        学生id:<asp:TextBox ID="txtstudentid2" runat="server"></asp:TextBox>
   
    </div>
    </form>
</body>
</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.SqlClient;

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

        }

        protected void btncharu_Click(object sender, EventArgs e)
        {
            string classid=txtclassid.Text;
            string name=txtname.Text;
            string gender=txtgender.Text;
            string age=txtage.Text;
            string birthary=txtbirthary.Text;
            string photo = txtPhone.Text;
            string Cardid = txtCardid.Text;
            string constr = "data source=.;initial catalog=TestSchool;user=sa;password=zhs19921013";
            using (SqlConnection conn = new SqlConnection(constr))
            {
                conn.Open();
                //string sql = "insert into TblStudent values(@tSClassid,@tSName,@tSAge,@tSGender,@tSBirthday)";
                //string sql2 = "select @@IDENTITY";
                using (SqlCommand cmd = conn.CreateCommand())
                {

                    cmd.CommandText = "insert into TblStudent output inserted.tSid values(@tSName,@tSGender,@tSPhone,@tSAge,@tSBirthday,@tSCardid,@tSClassid)";
                   
                    cmd.Parameters.Add(new SqlParameter("@tSName",name));
                    cmd.Parameters.Add(new SqlParameter("@tSGender", gender));
                    cmd.Parameters.Add(new SqlParameter("@tSPhone", photo));
                    cmd.Parameters.Add(new SqlParameter("@tSAge",age));
                 
                    cmd.Parameters.Add(new SqlParameter("@tSBirthday",birthary));
                  
                    cmd.Parameters.Add(new SqlParameter("@tSCardid", Cardid));
                    cmd.Parameters.Add(new SqlParameter("@tSClassid", classid));
                   
                    //cmd.CommandText = sql2;
                    int sql3=(int)cmd.ExecuteScalar();
                    txtstudentid2.Text = sql3.ToString();
                    Response.Write(sql3);
                }
               
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值