应用存储过程添加数据
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body style="border-top- thin; border-left- thin; font-size: 12px; border-left-color: #000099; border-bottom- thin; border-bottom-color: #000099; border-top-color: #000099; border-right- thin; border-right-color: #000099">
<form id="form1" runat="server">
<div>
<table style="border-right: #000099 thin ridge; border-top: #000099 thin ridge; border-left: #000099 thin ridge;
259px; border-bottom: #000099 thin ridge; height: 1px">
<tr>
<td colspan="2" style="height: 17px; text-align: center">
<strong><span style="font-size: 12pt; color: #000099; text-decoration: underline">会员基本信息注册</span></strong></td>
</tr>
<tr>
<td style=" 233px">
会员编号:</td>
<td style=" 221px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style=" 233px">
会员姓名:</td>
<td style=" 221px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style=" 233px">
身份证号码:</td>
<td style=" 221px">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style=" 233px">
联系电话:</td>
<td style=" 221px">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style=" 233px">
</td>
<td style=" 221px; text-align: center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="注 册" /></td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strCon"]);
con.Open();
SqlCommand cmd = new SqlCommand("procInsertEmployee",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parms = { new SqlParameter("@员工编号",SqlDbType.VarChar,50),
new SqlParameter("@员工姓名",SqlDbType.VarChar,50),
new SqlParameter("@身份证号",SqlDbType.VarChar,50),
new SqlParameter("@联系电话",SqlDbType.VarChar,50)
};
parms[0].Value = TextBox1.Text;
parms[1].Value = TextBox2.Text;
parms[2].Value = TextBox3.Text;
parms[3].Value = TextBox4.Text;
foreach(SqlParameter parameter in parms)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(''注册成功!'')</script>");
BindData();
}
public void BindData()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strCon"]);
SqlDataAdapter da = new SqlDataAdapter("getAllEmployee",con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds,"table");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
//删除数据
应用存储过程删除数据
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<body style="font-size: 12px">
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" Style="border-right: #cccccc thin groove;
border-top: #cccccc thin groove; border-left: #cccccc thin groove; border-bottom: #cccccc thin groove"
Width="375px">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="员工编号" HeaderText="员工编号" />
<asp:BoundField DataField="员工姓名" HeaderText="员工姓名" />
<asp:BoundField DataField="身份证号" HeaderText="身份证号" />
<asp:BoundField DataField="联系电话" HeaderText="联系电话" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
SqlDataAdapter dap = new SqlDataAdapter("select * from 员工信息表", con);
DataSet ds = new DataSet();
dap.Fill(ds, "table");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "员工编号" };
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);//创建数据库连接
con.Open();//打开数据库
SqlCommand cmd = new SqlCommand("procDeleteEmployee", con);//调用执行删除的存储过程
cmd.CommandType = CommandType.StoredProcedure;//设定数据操作类型
SqlParameter pares = new SqlParameter("@员工编号", SqlDbType.VarChar, 50);
cmd.Parameters.Add(pares);//添加参数
cmd.Parameters["@员工编号"].Value = GridView1.DataKeys[e.RowIndex].Value.ToString();//传值操作
cmd.ExecuteNonQuery();//执行删除操作
con.Close();//关闭连接
this.Page_Load(sender, e);
}
}