• 个人学习代码保存:例9.在存储过程中使用cast 类型转换的实例


    存储过程:
    ALTER PROCEDURE dbo.GetTitleTop
        
        (
        
    @kindid int,
        
    @IntTop int
        )
        
    AS

        
    declare @sql varchar(200)
        
    set @sql='select top '+cast(@IntTop as varchar)+' * from guestbook where kindid='+cast(@kindid as varchar
        
    EXEC (@sql)
        
    RETURN
    前台代码:
    <%@ 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>
            KindId:
    <asp:TextBox ID="txtKindId" runat="server"></asp:TextBox>
            
    <br />
            条数:
    &nbsp; &nbsp;<asp:TextBox ID="txtIntTop" runat="server"></asp:TextBox><br />
            
    <asp:Button ID="Button1" runat="server" Text="查看" OnClick="Button1_Click1" />
            
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                ForeColor
    ="#333333" GridLines="None">
                
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                
    <Columns>
                    
    <asp:BoundField DataField="id" HeaderText="序号" />
                    
    <asp:BoundField DataField="kindId" HeaderText="类型编号" />
                    
    <asp:BoundField DataField="title" HeaderText="标题" />
                    
    <asp:BoundField DataField="body" HeaderText="内容" />
                
    </Columns>
                
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                
    <AlternatingRowStyle BackColor="White" />
            
    </asp:GridView>
        
    </div>
        
    </form>
    </body>
    </html>
    后台代码:
    using System;
    using System.Data;
    using System.Configuration;
    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 _Default : System.Web.UI.Page 
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }

       
        
    protected void Button1_Click1(object sender, EventArgs e)
        
    {
            
    string connstr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            SqlConnection con 
    = new SqlConnection(connstr);
            
    if (con.State.Equals(ConnectionState.Closed))
            
    {
                con.Open();
            }

            
    string procName = "dbo.GetTitleTop";
            SqlCommand cmd 
    = new SqlCommand(procName, con);
            cmd.CommandType 
    = CommandType.StoredProcedure;
            cmd.Parameters.Add(
    "@kindid", SqlDbType.Int);
            cmd.Parameters.Add(
    "@IntTop", SqlDbType.Int);
            cmd.Parameters[
    0].Value = Convert.ToInt32(this.txtKindId.Text.Trim());
            cmd.Parameters[
    1].Value = Convert.ToInt32(this.txtIntTop.Text.Trim());
            SqlDataReader sdr 
    = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            
    this.GridView1.DataSource = sdr;
            
    this.GridView1.DataBind();
        }

    }

  • 相关阅读:
    Blink示例程序
    arduino入门笔记
    Opentrains 1519 G——最小圆覆盖
    最小圆覆盖入门
    最小球覆盖——模拟退火&&三分套三分套三分
    DAO,Service,Controller各层之间的关系
    史上最全的SpringMVC学习笔记
    pom.xml
    Tomcat入门指南
    tomcat架构
  • 原文地址:https://www.cnblogs.com/wbcms/p/1037564.html
Copyright © 2020-2023  润新知