insert.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert.aspx.cs" Inherits="insert" %>
<!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 align=center>
<asp:Panel ID="Panel1" runat="server" Height="615px" Width="697px">
<asp:Label ID="Label1" runat="server" Height="23px" Text="插入数据示例" Width="160px"></asp:Label><div
style="text-align: left">
<table align=center style="width: 80%; height: 34%" id="TABLE1" runat="server">
<tr>
<td style="width: 33px; height: 3px">
</td>
<td style="width: 125px; height: 3px">
</td>
<td style="width: 33px; height: 3px">
</td>
<td style="width: 125px; height: 3px">
</td>
</tr>
<tr>
<td style="width: 33px; height: 13px;">
学号</td>
<td style="width: 125px; height: 13px;">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td style="width: 33px; height: 13px;">
姓名</td>
<td style="width: 125px; height: 13px;">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 33px">
语文</td>
<td style="width: 125px">
<asp:TextBox ID="TextBox3" runat="server" Height="15px" Width="50px"></asp:TextBox></td>
<td style="width: 33px">
数学</td>
<td style="width: 125px">
<asp:TextBox ID="TextBox4" runat="server" Height="15px" Width="50px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 33px">
英语</td>
<td style="width: 125px">
<asp:TextBox ID="TextBox5" runat="server" Height="15px" Width="50px"></asp:TextBox></td>
<td style="width: 33px">
政治</td>
<td style="width: 125px">
<asp:TextBox ID="TextBox6" runat="server" Height="15px" Width="50px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 33px">
</td>
<td style="width: 125px">
</td>
<td style="width: 33px">
</td>
<td style="width: 125px">
<asp:Button ID="Button1" runat="server" Text="插入学生成绩" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
<table align="center" style="width: 80%; height: 38%" id="TABLE2" runat="server">
<tr>
<td align="left" style="width: 48px; height: 33px">
</td>
<td align="left" style="width: 116px; height: 33px">
<asp:Label ID="Label2" runat="server" Height="19px" Width="220px"></asp:Label></td>
<td align="left" style="width: 38px; height: 33px">
</td>
<td align="left" style="width: 125px; height: 33px">
</td>
</tr>
<tr>
<td align="left" style="width: 48px">
通讯地址</td>
<td align="left" style="width: 116px">
<asp:TextBox ID="TextBox7" runat="server" Height="15px" Width="199px"></asp:TextBox></td>
<td align="left" style="width: 38px">
邮政<br />
编码</td>
<td align="left" style="width: 125px">
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td align="left" style="width: 48px; height: 31px">
电话</td>
<td align="left" style="width: 116px; height: 31px">
<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox></td>
<td align="left" style="width: 38px; height: 31px">
Email</td>
<td align="left" style="width: 125px; height: 31px">
<asp:TextBox ID="TextBox10" runat="server" Height="15px" Width="185px"></asp:TextBox></td>
</tr>
<tr>
<td align="left" colspan="4" style="height: 149px">
备注
<p></p>
<asp:TextBox ID="TextBox11" runat="server" Height="55px" Width="421px" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 48px">
</td>
<td style="width: 116px">
</td>
<td style="width: 38px">
</td>
<td style="width: 125px">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="插入学生信息" /></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
Height="144px" Width="659px">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Panel>
<p>
</p>
</div></form>
</body>
</html>
insert.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class insert : System.Web.UI.Page
{
protected SqlConnection myConnection = new SqlConnection();
private void Page_Load(object sender, EventArgs e)
{
string strConn = "server=localhost;uid=sa;pwd=860712;database=MMS";
myConnection.ConnectionString = strConn;
TABLE1.Visible = true;
TABLE2.Visible = false;
if (!IsPostBack)
{
BindGrid("grade");
}
}
private void BindGrid(string tableName)
{
//根据参数tableName,决定访问的表
string strCommand = "select * from " + tableName;
SqlDataAdapter da = new SqlDataAdapter(strCommand, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "scores");
GridView1.DataSource = ds.Tables["scores"].DefaultView;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//将Table中所添加内容插入到表grade中
int t3 =Convert.ToInt32(TextBox3.Text);
int t4 = Convert.ToInt32(TextBox4.Text);
int t5 = Convert.ToInt32(TextBox5.Text);
int t6 = Convert.ToInt32(TextBox6.Text);
int c = Convert.ToInt32(t3 + t4 + t5 + t6);
float ave =c/4;
string strCommand = "insert grade values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + t3 + "','" + t4 + "','" + t5 + "','" + t6 + "','"+ave+"')";
SqlCommand addScore = new SqlCommand(strCommand, myConnection);
myConnection.Open();
addScore.ExecuteNonQuery();
myConnection.Close();
BindGrid("student");
Label1.Text = "学生成绩插入成功!请插入学生信息";
Label2.Text = TextBox2.Text;
TABLE1.Visible = false;
TABLE2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
string strCommand = "insert student values('" + TextBox1.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "')";
SqlCommand addInfo = new SqlCommand(strCommand, myConnection);
myConnection.Open();
addInfo.ExecuteNonQuery();
myConnection.Close();
Response.Redirect("insert.aspx");
TABLE1.Visible = true;
TABLE2.Visible = false;
}
}