黑马程序员_带参数存储过程入门学习

来源:互联网 发布:天猫 淘宝商城床单人床 编辑:程序博客网 时间:2024/04/29 19:34
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

今天学习了存储过程入门,其实感觉储存过程没有什么,就是把SQL语句集合到一起了,

html页面代码功能
<%@ 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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblShow" runat="server"></asp:Label>
    <asp:Button ID="btnShow" runat="server" Text="Test" onclick="btnShow_Click"/>
    <table border="1" align="center" width="600" style="text-align:center;">
    <asp:Repeater ID="repeater" runat="server">
     <HeaderTemplate>
     <tr style="background-color:skyblue;color:white;">
     <td style="width:90px;">
     用户编号
     </td>
      <td style="width:90px;">
     用户名
     </td>
      <td style="width:90px;">
     用户密码
     </td>
      <td style="width:90px;">
     年龄
     </td>
     <td style="width:90px;">
     性别
     </td>
     </tr>
     </HeaderTemplate>
    <ItemTemplate>
     <tr>
    <td style="width:90px;">
    <%#Eval("ID") %>
    </td>
    <td style="width:90px;">
    <%#Eval("UserName") %>
    </td>
    <td style="width:90px;">
    <%#Eval("UserPass") %>
    </td>
    <td style="width:90px;">
    <%#Eval("Age") %>
    </td>
    <td style="width:90px;">
    <%#Eval("Role") %>
    </td>
    </tr>
      </ItemTemplate>
       </asp:Repeater>
     </table>
    </div>
    </form>
</body>
</html>
后台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;
using System.Data.SqlClient;
using System.Data.Sql;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnShow_Click(object sender, EventArgs e)
    { 
        SqlConnection con = new SqlConnection("Server=.;database=db_test;pwd=;uid=sa;");
        SqlParameter para = null;
        int startAge = 21;
        DataTable dt = new DataTable();
        SqlDataAdapter sda=new SqlDataAdapter("db_test1",con);
        para=new SqlParameter("@startAge",startAge);
        sda.SelectCommand.Parameters.Add(para);
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        try
        {
            con.Open();
            sda.Fill(dt);
            repeater.DataSource = dt;
            repeater.DataBind();//数据绑定
            con.Close();
        }
        catch (Exception ee)
        {
            Response.Write("<script>alert('出现了异常!')</script>");
        }
    }
}
数据库存储过程
CREATE procedure db_test1
@startAge int
as
 select * from tb_test where Age>@startAge order by Age Desc
GO


 

 

 

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------详细请查看:http://net.itheima.com/
原创粉丝点击