用带参数的存储过程实现新闻的搜索功能

来源:互联网 发布:淘宝网兔子 编辑:程序博客网 时间:2024/06/06 03:38

--这是数据库中的存储过程

CREATE PROC Pro_GetNewsByTitle2
@newkey varchar(64),
@count int out
AS
select NewsTitle,SUBSTRING(NewsContent,1,20)+'......' as NewsContent,CreateTime from T_News WHERE NewsTitle LIKE @newkey Or NewsContent like @newkey;
select @count=COUNT(*) from T_News WHERE NewsTitle LIKE @newkey Or NewsContent like @newkey;
GO 

--这是新闻搜索功能的前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="新闻管理系统.WebForm1" %>


<!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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1"
            runat="server" Text="搜索" onclick="Button1_Click" /><asp:Label ID="Label1" runat="server"
                Text="Label"></asp:Label>
            <div id="divResult" runat="server"></div>
    </div>
    </form>
</body>
</html>

--这是新闻搜索功能的后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;


namespace 新闻管理系统
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateBind();
            }
        }


        private void DateBind()
        {
            string connstr = "data source=LOVE-PC\\SQLEXPRESSPC;initial catalog=News;user id=sa;password=admin";
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT NewsTitle,SUBSTRING(NewsContent,1,20)+'....' AS NewsContent,CreateTime From T_News WHERE NewsTitle LIKE @newskey OR NewsContent LIKE @newskey";
                    cmd.CommandText = "Pro_GetNewsByTitle2";
                  /*参数替换方法1*/
                    cmd.Parameters.AddWithValue("@newkey", "%" + TextBox1.Text + "%");
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter pm1 = new SqlParameter("@count", SqlDbType.Int);
                    pm1.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(pm1);
                    ////参数替换方法2
                    //SqlParameter pmd = new SqlParameter("@newskey", "%" + TextBox1.Text + "%");
                    //cmd.Parameters.Add(pmd);
                    ////参数替换三
                    //SqlParameter pm1 = new SqlParameter("@newskey",SqlDbType.VarChar);
                    //pm1.Value = "%" + TextBox1.Text + "%";
                    //cmd.Parameters.Add(pm1);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    //html只认html控件,所有服务器端控件都将转换为html控件            
                    int count = adapter.Fill(dt);
                    Label1.Text = "为您找到" + pm1.Value.ToString()+"条符合条件的记录";
                    //Response.Write(count);
                    //使用Fill方法填充数据的时候,不需要显示打开连接,因为在Fill方法内部有判断 if(conn.open==false){conn.open()}
                    cmd.Dispose();
                    conn.Dispose();


                    StringBuilder sb1 = new StringBuilder();
                    sb1.Append("<table border=2>");
                    sb1.Append("<tr><td>新闻标题</td><td>新闻内容</td><td>创建时间</td></tr>");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sb1.Append("<tr>");
                        sb1.Append("<td>"+dt.Rows[i]["NewsTitle"].ToString()+"</td>");
                        sb1.Append("<td>" + dt.Rows[i]["NewsContent"].ToString() + "</td>");
                        sb1.Append("<td>" + dt.Rows[i]["CreateTime"].ToString() + "</td>");
                        sb1.Append("</tr>");
                        
                    }
                    sb1.Append("</table>");
                    divResult.InnerHtml = sb1.ToString();


                
                }
           
           }
        
        
        
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DateBind();
        }
    }
}

原创粉丝点击