<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);
}
}
}
}
}