使用存储过程实现显示搜索结果和数据条数
2013-03-11 17:45
330 查看
存储过程:
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>
<asp:Button ID="btnQuery" runat="server" Text="搜索" onclick="btnQuery_Click" /> 共搜索到了<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();
}
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>
<asp:Button ID="btnQuery" runat="server" Text="搜索" onclick="btnQuery_Click" /> 共搜索到了<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();
}
相关文章推荐
- 使用DataList和SQL2005实现分页显示数据(应用举例:站内简单搜索)
- SQL Server2000 索引结构及其使用 (实现小数据量和海量数据的通用分页显示存储过程)
- easyui-combobox---ajax获取数据库JSON数据,实现搜索框实时显示模糊搜索结果
- 实现小数据量和海量数据的通用分页显示存储过程
- 使用DataGrid控件实现以主细表显示数据
- 实现小数据量和海量数据的通用分页显示存储过程
- 五滴水:使用Ajax+jQuery来实现前端收到的数据在console上显示+简单的主页设计与bootstrap插件实现图片轮播
- 实现在搜索后将结果中的关键字高亮显示
- sql 优化之:实现小数据量和海量数据的通用分页显示存储过程(系列四)
- 使用正则表达式实现搜索关键字高亮显示
- sql 优化之:实现小数据量和海量数据的通用分页显示存储过程(系列四)
- [置顶] ExtJs4.2应用:使用ExtJs扩展组件searchfield实现数据搜索功能
- 使用react实现手机号的数据同步显示功能的示例代码
- 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索
- 『原创』使用ASP与JAVASCRIPT配合实现多个复选框数据关联显示
- Matlab作图后,使用data cursor实现显示多个点的数据值
- 使用后台代码实现Gridview显示sql数据
- 如何优化操作大数据量数据库(几十万以上数据)(三。实现小数据量和海量数据的分页显示存储过程)
- win8使用every'thing无法显示搜索结果的解决方法
- 使用存储过程实现数据的交换