<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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>
<br />
<asp:Label ID="Label1" runat="server" Text="添加数据:"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="学号:"></asp:Label>
<asp:TextBox ID="StuNo" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="姓名:"></asp:Label>
<asp:TextBox ID="StuName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label4" runat="server" Text="年龄:"></asp:Label>
<asp:TextBox ID="StuAge" runat="server"></asp:TextBox>
<br />
<asp:Button ID="AddButton" runat="server" Text="添加" οnclick="AddButton_Click" />
<asp:Label ID="AddMsg" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Label ID="Label5" runat="server" Text="修改数据:"></asp:Label>
<br />
<asp:Label ID="Label6" runat="server" Text="学号:"></asp:Label>
<asp:TextBox ID="StuNo2" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label7" runat="server" Text="姓名:"></asp:Label>
<asp:TextBox ID="StuName2" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label8" runat="server" Text="年龄:"></asp:Label>
<asp:TextBox ID="StuAge2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="UpdateButton" runat="server" Text="更新"
οnclick="UpdateButton_Click"/>
<asp:Label ID="UpdateMsg" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label9" runat="server" Text="删除数据:"></asp:Label>
<br />
<asp:Label ID="Label10" runat="server" Text="学号:"></asp:Label>
<asp:TextBox ID="StuNo3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="DeleteButton" runat="server" Text="删除"
οnclick="DeleteButton_Click" style="width: 40px"
/>
<asp:Label ID="DeleteMsg" runat="server"></asp:Label>
<br />
<br />
<asp:sqldatasource runat="server"
id = "SqlDataSourceForTestDB"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:ConnectionStringForTestDB %>"
SelectCommand="SELECT * FROM [MyTable]"
InsertCommand="insert into mytable values(@no,@name,@age);"
DeleteCommand="delete from MyTable where stuno=@no;"
UpdateCommand="update mytable set stuname=@name,stuage=@age where stuno=@no;"
>
</asp:sqldatasource>
</div>
</form>
</body>
</html>
代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AddButton_Click(object sender, EventArgs e)
{
string no = StuNo.Text;
string name = StuName.Text;
string age = StuAge.Text;
int age2 = 0;
if (String.IsNullOrEmpty(no) || String.IsNullOrEmpty(name) || String.IsNullOrEmpty(age))
{
AddMsg.Text = " " + "数据项不能为空";
return;
}
age2 = Convert.ToInt32(age);
SqlDataSourceForTestDB.InsertParameters.Add("no", no);
SqlDataSourceForTestDB.InsertParameters.Add("name", name);
SqlDataSourceForTestDB.InsertParameters.Add("age", age);
if (SqlDataSourceForTestDB.Insert() > 0)
{
AddMsg.Text = " " + "数据添加成功";
return;
}
AddMsg.Text = " " + "数据添加失败";
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
string no = StuNo2.Text;
string name = StuName2.Text;
string age = StuAge2.Text;
int age2 = 0;
if (String.IsNullOrEmpty(no) || String.IsNullOrEmpty(name) || String.IsNullOrEmpty(age))
{
UpdateMsg.Text = " " + "数据项不能为空";
return;
}
age2 = Convert.ToInt32(age);
SqlDataSourceForTestDB.UpdateParameters.Add("no", no);
SqlDataSourceForTestDB.UpdateParameters.Add("name", name);
SqlDataSourceForTestDB.UpdateParameters.Add("age", age);
if (SqlDataSourceForTestDB.Update() > 0)
{
UpdateMsg.Text = " " + "数据更新成功";
return;
}
UpdateMsg.Text = " " + "数据更新失败";
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
string no = StuNo3.Text;
if (String.IsNullOrEmpty(no))
{
DeleteMsg.Text = " " + "数据项不能为空";
return;
}
SqlDataSourceForTestDB.DeleteParameters.Add("no", no);
if (SqlDataSourceForTestDB.Delete() > 0)
{
DeleteMsg.Text = " " + "数据删除成功";
return;
}
DeleteMsg.Text = " " + "数据删除失败";
}
}
}