运用数据库查询语句并且返回查询出来返回的查询条数
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();
}
}
<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();
}
}
相关文章推荐
- sql server 添加语句中,VALUES值需要从数据库中查询出来
- 如何解决返回数据库查询纪录条数的…
- TP 查询时候标题带有&的字符,查询没有结果!打出SQL语句去数据库查,返回有结果!
- 程序执行hibernate语句查询到数据库里的char型字段,返回的只有该字段的首字母
- PHP mysqul连接数据库,执行查询语句,打印返回结果集和断开数据库
- 给数据库字段添加注释,并且可以通过sql语句查询
- 给数据库字段添加注释,并且可以通过sql语句查询
- 给数据库字段添加注释,并且可以通过sql语句查询
- SQL语句查询:把数据库中的表(表名)列出来
- 给数据库字段添加注释,并且可以通过sql语句查询
- 在数据库查询语句中 要返回 一个集合的数据 的处理方式。
- 查询数据库的 sql语句 返回字段 和类 属性的关系
- SQL语句返回多表联合查询的结果集的数据条数
- 在hibernate3中如何利用HQL语句查询出对象中的部分数据并且返回该对象?
- 求mysql倒序并且限制查询的条数的查询语句?
- mysql 查询数据库中的存储过程与函数的语句
- 各种数据库实现随机查询语句,网上转载,稍带个人总结...
- 在SQL语句中查询多条记录并把多条记录同一字段的值连接成一个字符并返回的写法
- 数据库insert and update--查询是否主键存在和插入语句用一条sql来实现。
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。