使用存储过程实现显示搜索结果和数据条数

本文展示了如何利用存储过程Pro_GetNewsByTitleAndContentNumb在SQL中搜索新闻标题和内容,并同时计算匹配的数据条数。在前端,用户输入搜索关键字,后台ASP.NET代码连接数据库执行存储过程,获取新闻标题、内容片段和创建时间,以及搜索结果的数量。搜索结果以表格形式展示给用户。

存储过程:

CREATE PROC Pro_GetNewsByTitleAndContentNumb
@newsKey VARCHAR(64),
@numb int out
AS
SELECT NewsTitle,SUBSTRING(NewsContent,1,20)+'......' as NewsContent,CreateTime FROM T_News
WHERE NewsTitle like @newsKey or NewsContent like @newsKey;
SELECT @numb=COUNT(*)FROM T_News
WHERE NewsTitle like @newsKey or NewsContent like @newsKey;

Go

 

 

前台代码:

<body>
    <form id="form1" runat="server">
    <div>标题:<asp:TextBox ID="txtNewsTitle" runat="server" ></asp:TextBox>
&nbsp;<asp:Button ID="btnQuery" runat="server" Text="搜索" onclick="btnQuery_Click" />&nbsp;&nbsp; 共搜索到了<asp:Label
    ID="Label1" runat="server" Text=""></asp:Label>条数据

    <div id="divResult" runat="server">
        </div>
    </div>
    </form>
</body>

 

后台代码:

 string conStr = @"Data Source=.;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=111111";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataLoad();
            }
        }

        private void DataLoad()
        {
            #region 根据用户的输入获取数据
            SqlConnection conn = new SqlConnection(conStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "Pro_GetNewsByTitleAndContentNumb";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@newsKey", "%" + txtNewsTitle.Text + "%");

            #region output参数
            SqlParameter param1 = new SqlParameter("@numb", SqlDbType.Int);
            cmd.Parameters.Add(param1);
            param1.Direction = ParameterDirection.Output;
            #endregion
           
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            StringBuilder sb1 = new StringBuilder();
            #endregion


            #region 将数据拼接成字符串发送到前台
            Label1.Text = (param1.Value).ToString();
            string newscontent = string.Empty;
            string newstitle = string.Empty;
            sb1.Append("<table border=2");
            sb1.Append("<tr><td>标题</td><td>内容</td><td>创建时间</td></tr>"); 
            foreach (DataRow row in dt.Rows)
            {
                sb1.Append("<tr>");
                newstitle = row["NewsTitle"].ToString();
                sb1.Append("<td>" + newstitle + "</td>");
                newscontent = row["NewsContent"].ToString();
                sb1.Append("<td>" + newscontent + "</td>");
                sb1.Append("<td>" + Convert.ToDateTime(row["CreateTime"].ToString()).ToString("yyyy-MM-dd hh-mm-ss") + "</td>");
                sb1.Append("</tr>");
            }
            sb1.Append("</table>");
            divResult.InnerHtml = sb1.ToString();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Dispose();
            #endregion
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值