建表并对表进行增删改查

创建学生信息表并实现添加,查询和更改功能

------------------------------创建学生成绩表T_Students

CREATE TABLE T_Students(
StudentId uniqueidentifier,
SName nvarchar(10),
ClassId int,
TName nvarchar(10),
Sphone varchar(15),
SAddress nvarchar (50)
)

-----------------------------前台代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 194px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 50%;">
<tr>
<td>
&nbsp;
学生编号:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtsid" runat="server" ReadOnly="true" Enabled="False"></asp:TextBox>
&nbsp;</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
&nbsp;
学生姓名:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtsname" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;
班级编号:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtclassid" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
&nbsp;班主任姓名:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtteachername" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
&nbsp;
联系电话:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
&nbsp;
家庭住址:</td>
<td class="style1">
&nbsp;
<asp:TextBox ID="txtadress" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;
</td>
</tr>
<tr>
<td>
&nbsp;
<asp:Button ID="btnadd" runat="server" Xοnclick="btnadd_Click" Text="添加" />
</td>
<td class="style1">
&nbsp;
<asp:Button ID="btnselect" runat="server" Text="查询" Xοnclick="btnselect_Click" />
</td>
<td>
&nbsp;
<asp:Button ID="btnupdate" runat="server" Text="更改" Xοnclick="btnupdate_Click" />
</td>
</tr>
</table>
</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.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace 新闻系统
{
public partial class Students : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

-----------------------------------添加

protected void btnadd_Click(object sender, EventArgs e)
{
string studentid=txtsid.Text.Trim();
string sname =txtsname.Text.Trim();
string classid=txtclassid.Text.Trim();
string tname=txtteachername.Text.Trim();
string sphone=txtphone.Text.Trim();
string saddress=txtadress.Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert into T_Students values(NEWID(),@SName,@ClassId,@TName,@Sphone,@SAddress)";
cmd.Parameters.Add(new SqlParameter("@SName", sname));
cmd.Parameters.Add(new SqlParameter("@ClassId", classid));
cmd.Parameters.Add(new SqlParameter("@TName", tname));
cmd.Parameters.Add(new SqlParameter("@Sphone",sphone));
cmd.Parameters.Add(new SqlParameter("@SAddress", saddress));
cmd.ExecuteNonQuery();
Response.Write("添加成功!");
}
}
}

-----------------------查询

protected void btnselect_Click(object sender, EventArgs e)
{

string sname = txtsname.Text.Trim();

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select StudentId,SName,ClassId,Tname,Sphone,SAddress from T_Students where
SName=@SName";
cmd.Parameters.Add(new SqlParameter("@SName",sname));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adapter.Fill(dt);
//Response.Write( dt.Rows.Count);
txtsid.Text = dt.Rows[0]["StudentId"].ToString();
txtsname.Text = dt.Rows[0]["SName"].ToString();
txtclassid.Text = dt.Rows[0]["ClassId"].ToString();
txtteachername.Text = dt.Rows[0]["Tname"].ToString();
txtphone.Text = dt.Rows[0]["Sphone"].ToString();
txtadress.Text = dt.Rows[0]["SAddress"].ToString();
}

}
}
}

------------------------------修改

protected void btnupdate_Click(object sender, EventArgs e)
{
string studentid = txtsid.Text.Trim();
string sname = txtsname.Text.Trim();
string classid = txtclassid.Text.Trim();
string tname = txtteachername.Text.Trim();
string sphone = txtphone.Text.Trim();
string saddress = txtadress.Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update T_Students set
SName=@sname,ClassId=@classid,TName=@tname,Sphone=@sphone,SAddress=@saddress where StudentId=@StudentId";
cmd.Parameters.Add(new SqlParameter("@sname", sname));
cmd.Parameters.Add(new SqlParameter("@classid", classid));
cmd.Parameters.Add(new SqlParameter("@tname", tname));
cmd.Parameters.Add(new SqlParameter("@sphone", sphone));
cmd.Parameters.Add(new SqlParameter("@saddress", saddress));
cmd.Parameters.Add(new SqlParameter("@StudentId",studentid));
cmd.ExecuteNonQuery();
}
}

}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值