您的位置:首页 > 数据库

运用数据库查询语句并且返回查询出来返回的查询条数

2013-03-12 17:48 453 查看
===========前台========

<body>

<form id="form1" runat="server">

<div>

新闻标题:<asp:TextBox ID="txtNewsTitle" runat="server"></asp:TextBox>

<asp:ImageButton ID="ImageButton1" runat="server"

ImageUrl="~/3-12调试/images/1.jpg" Xonclick="ImageButton1_Click" />

<div id="divResule" runat="server"></div>

</div>

</form>

</body>

===========后台=========

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DataLoad();

}

}

string strcon = @"data source=.;initial catalog=数据库名;user id=连接数据库的名;password=连接数据库的密码";

private void DataLoad()

{

SqlConnection con = new SqlConnection(strcon);

con.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "select NewsTitle,NewsContent,NewsCreator from T_News where NewsTitle like @maxid or NewsContent like @maxid";

cmd.Parameters.AddWithValue("@maxid", "%" + txtNewsTitle.Text + "%");

SqlDataAdapter adpter = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

adpter.Fill(dt);
StringBuilder sb1 = new StringBuilder();

sb1.Append("<table>");

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]["NewsCreator"].ToString() + "</td>");

sb1.Append("</tr>");

}

sb1.Append("</table>");

divResule.InnerHtml = sb1.ToString();

con.Dispose();

cmd.Dispose();

}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

{

DataLoad();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐