ASP.NET利用存储过程查询、编辑、修改、删除

前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="use_ProName.aspx.cs" Inherits="use_ProName" %>
<!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>
        <asp:Label ID="Label1" runat="server" Text="请输入id号:(如200401):"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server"  Text="查询" />
              <asp:GridView ID="GridView1" runat="server" Height="140px" Width="100%" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" 
                          DataKeyNames="id" Horiz>
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                                           
                                <asp:BoundField HeaderText="编号" FooterText="编号" DataField="id" SortExpression="id" />
                                <asp:BoundField HeaderText="姓名" FooterText="姓名" DataField="uname" SortExpression="uname" />
                                <asp:BoundField HeaderText="性别" FooterText="性别" DataField="sex" SortExpression="sex" />
                                <asp:BoundField HeaderText="年龄" FooterText="年龄" DataField="age" SortExpression="age" />
                                <asp:BoundField HeaderText="电话" FooterText="电话" DataField="telphone" SortExpression="telphone" />                               
                                <asp:BoundField HeaderText="住址" FooterText="住址" DataField="address" SortExpression="address" />
                                <asp:BoundField HeaderText="职位" FooterText="职位" DataField="ziwei" SortExpression="ziwei" />
                                <asp:BoundField HeaderText="自我评价" FooterText="自我评价" DataField="pingjia" SortExpression="pingjia" />
                                <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                              <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                            </Columns>
                           
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" Horiz />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                </asp:GridView>
   
    </div>
    </form>
</body>
</html>

后台代码:
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 use_ProName : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         
    }
    protected void GetInfoByName(string ids)
    {
        string id = ids;
        SqlConnection conn = new SqlConnection("server=localhost;database=huiyuan;uid=sa;pwd=sa");
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure; //指定执行存储过程操作
        cmd.CommandText = "ProName"; //存储过程名称
        //对应存储过程QueryInfoByName的第一个参数@name
        SqlParameter parid = new SqlParameter("@id", SqlDbType.Char, 10);
        //指定参数@name要转入的值
        parid.Value = TextBox1.Text;
        //对应存储过程QueryInfoByName的第二个参数@age
      // SqlParameter parAge = new SqlParameter("@age", SqlDbType.Int);
        //指定参数@age要转入的值
      // parAge.Value = txt_age;
        //这一步非常重要,一定将设置好的两个参数类型添加到Command对象的参数集合里
        cmd.Parameters.Add(parid);
        //cmd.Parameters.Add(parAge);
        //方式一,查询回来的结果需要显示在DataGrid之类的控件上
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Clear();
        ds.Dispose();
     
        //方式二,按单个值读取
    //    conn.Open();
      // SqlDataReader reader = cmd.ExecuteReader();
      // if (reader.HasRows)
      // {
      //    while (reader.Read())
        //    {
        //      Response.Write(reader.GetString(0));
          // }
      // }//
      // conn.Close();
    }
      protected void Button1_Click(object sender, EventArgs e)
    {
        string id = TextBox1.Text;
        GetInfoByName(id);
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        string id = TextBox1.Text;
        GetInfoByName(id);
    }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string connstr = System.Configuration.ConfigurationManager.AppSettings["conn"];
        // 建立连接
        SqlConnection sconn = new SqlConnection(connstr);
        string sqlstr = "delete from huiyuan where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sconn = new SqlConnection(connstr);
        SqlCommand sqlcom = new SqlCommand(sqlstr, sconn);
        sconn.Open();
        sqlcom.ExecuteNonQuery();
        sconn.Close();
        string id = TextBox1.Text;
        GetInfoByName(id);
    }

    //更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string connstr = System.Configuration.ConfigurationManager.AppSettings["conn"];
        // 建立连接
        SqlConnection sconn = new SqlConnection(connstr);
        string sqlstr = "update huiyuan set uname='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',address='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',ziwei='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        SqlCommand sqlcom = new SqlCommand(sqlstr, sconn);
        sconn.Open();
        sqlcom.ExecuteNonQuery();
        sconn.Close();
        GridView1.EditIndex = -1;
        string id = TextBox1.Text;
        GetInfoByName(id);
    }

    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        string id = TextBox1.Text;
        GetInfoByName(id);
    }


}

华纬教育网www.hwjy.net.cn提供IT、外语学习、管理咨询类社区