1.把数据库里面的数据显示出来
sqlHelper怎么用:[网上可以下载,需要可以找楼主要]
1.拷贝到项目,修改它的命名空间等于当前项目名称
2.数据库的连接信息,用户名,密码,登录方式等
<connectionStrings>
<add name="con" connectionString="Database=mySchool;Server=.;Integrated Security=false;Uid=sa;Password=123456;" providerName="System.Data.SqlClient"/>
</connectionStrings>
1 // 1.怎么运用sqlhelper得到一个数据表,用一个变量存储起来 2 string strsql = "select NewsId,title,newsclass,ViewCount from RNews where 1=1"; 3 //2.找到数据库里sql语句对应的数据库表,一个dataset里有多个数据表 4 DataTable dt=SqlHelper.ExecuteDataSetText(strsql,null).Tables[0]; 5 GridView1.DataSource = dt; 6 GridView1.DataBind();
html,前端页面,用于接收数据的页面
1 <div id="gvRnews"> 2 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 3 <Columns> 4 <asp:BoundField DataField="NewsId" HeaderText="编号" /> 5 <asp:BoundField DataField="title" HeaderText="标题" /> 6 <asp:BoundField DataField="newsclass" HeaderText="班级" /> 7 <asp:BoundField DataField="ViewCount" HeaderText="浏览数" /> 8 </Columns> 9 </asp:GridView> 10 </div>
这样结果就出来啦:
2.通过标题和分类进行查询显示:
html部分代码:
1 <div id="gvSelect"> 2 <table> 3 <tr> 4 <td>标题:</td> 5 <td> 6 <asp:TextBox ID="txtSTitle" runat="server"></asp:TextBox> 7 </td> 8 <td>分类:</td> 9 <td> 10 <asp:DropDownList ID="ddlSNewsClass" runat="server"> 11 <asp:ListItem>--请选择--</asp:ListItem> 12 <asp:ListItem>首页焦点</asp:ListItem> 13 <asp:ListItem>业务信息</asp:ListItem> 14 <asp:ListItem>学员感言</asp:ListItem> 15 <asp:ListItem>常见问题</asp:ListItem> 16 </asp:DropDownList> 17 </td> 18 <td> 19 <asp:Button ID="btnSelect" runat="server" Text="查询" OnClick="btnSelect_Click" /> 20 </td> 21 </tr> 22 </table> 23 </div>
后台.cs代码,加上where 1=1的原因是为了什么条件下sql语句都正确,这很重要,不加的前提条件是它两都不能为空
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 BindRnews(); 4 } 5 //写在一个方法里,用的时候直接调用该方法 6 public void BindRnews() 7 { 8 //1.怎么运用sqlhelper得到一个数据表,用一个变量存储起来 9 string strsql = GetStrSql(); 10 //2.找到数据库里sql语句对应的数据库表 11 DataTable dt = SqlHelper.ExecuteDataSetText(strsql, null).Tables[0]; 12 GridView1.DataSource = dt; 13 GridView1.DataBind(); 14 } 15 public string GetStrSql() 16 { 17 StringBuilder sb = new StringBuilder(); 18 sb.Append("select NewsId,title,newsclass,ViewCount from RNews where 1=1"); 19 //如果它里面的字符串内容不等于空 20 if(!string.IsNullOrEmpty(txtSTitle.Text.Trim())) 21 { 22 sb.Append(string.Format("and title like '%{0}%' ",txtSTitle.Text.Trim())); 23 } 24 if (ddlSNewsClass.SelectedIndex > 0) 25 { 26 sb.Append(string.Format("and NewsClass like '%{0}%' ",ddlSNewsClass.SelectedValue)); 27 } 28 return sb.ToString(); 29 } 30 31 protected void btnSelect_Click(object sender, EventArgs e) 32 { 33 BindRnews(); 34 }
好啦,这样子查询结果就出来啦,效果如上截图所示。
3.增加数据
1 <div> 2 <table> 3 <tr> 4 <td>标题:</td> 5 <td> 6 <asp:TextBox ID="txtITitle" runat="server"></asp:TextBox> 7 </td> 8 <td>内容:</td> 9 <td> 10 <asp:TextBox ID="txtIText" runat="server"></asp:TextBox> 11 </td> 12 <td>分类:</td> 13 <td> 14 <asp:DropDownList ID="ddlINewsClass" runat="server"> 15 <asp:ListItem>--请选择--</asp:ListItem> 16 <asp:ListItem>首页焦点</asp:ListItem> 17 <asp:ListItem>业务信息</asp:ListItem> 18 <asp:ListItem>学员感言</asp:ListItem> 19 <asp:ListItem>常见问题</asp:ListItem> 20 </asp:DropDownList> 21 </td> 22 <td> 23 <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" /> 24 </td> 25 </tr> 26 </table> 27 </div>
.cs部分代码:
protected void btnAdd_Click(object sender, EventArgs e) { try { //1.获取到输入的标题,内容,选择的分类 string title = txtITitle.Text.Trim(); string text = txtIText.Text.Trim(); string rewsclass = ddlINewsClass.SelectedIndex > 0 ? ddlINewsClass.SelectedValue : ""; string strsql = string.Format("insert into RNews (Title,Text,CreatedTime,NewsClass) values ('{0}','{1}','{2}','{3}')", title, text, DateTime.Now.ToString(), rewsclass); //执行,判断有没有添加成功 if (SqlHelper.ExecteNonQueryText(strsql) > 0) { Response.Write("添加成功!"); } BindRnews(); } catch (Exception ex) { Response.Write("请联系管理员!"); } }
效果显示:
4.删除数据,通过Id删除数据
前端代码:
1 <div> 2 <table> 3 <tr> 4 <td>ID:</td> 5 <td> 6 <asp:TextBox ID="txtdDId" runat="server"></asp:TextBox> 7 </td> 8 <td> 9 <asp:Button ID="btnDelect" runat="server" Text="删除" OnClick="btnDelect_Click" /> 10 </td> 11 </tr> 12 </table> 13 </div>
.cs代码:
1 protected void btnDelect_Click(object sender, EventArgs e) 2 { 3 try 4 { 5 if (!string.IsNullOrEmpty(txtdDId.Text.Trim())) 6 { 7 int id = Convert.ToInt32(txtdDId.Text.Trim()); 8 string strsql1 = string.Format("select NewsId from RNews where NewsId='{0}'", id); 9 if (SqlHelper.Exists(strsql1)) 10 { 11 string strsql2 = string.Format("delete RNews where NewsId='{0}'", id); 12 if (SqlHelper.ExecteNonQueryText(strsql2) > 0) 13 { 14 Response.Write("删除成功!"); 15 } 16 BindRnews(); 17 } 18 else 19 { 20 Response.Write("Id不存在!"); 21 } 22 } 23 else 24 { 25 Response.Write("请输入内容!"); 26 } 27 } 28 catch 29 { 30 Response.Write("请联系管理员!"); 31 } 32 }
效果:
5.更新数据:
1 <div> 2 <table> 3 <tr> 4 <td>ID:</td> 5 <td> 6 <asp:TextBox ID="txtUId" runat="server"></asp:TextBox> 7 </td> 8 <td>标题:</td> 9 <td> 10 <asp:TextBox ID="txtUTitle" runat="server"></asp:TextBox> 11 </td> 12 <td> 13 <asp:Button ID="btnU" runat="server" Text="更新" OnClick="btnU_Click" /> 14 </td> 15 </tr> 16 </table> 17 </div>
.cs
1 protected void btnU_Click(object sender, EventArgs e) 2 { 3 try 4 { 5 if (!string.IsNullOrEmpty(txtUId.Text.Trim())) 6 { 7 int Id = Convert.ToInt32(txtUId.Text.Trim()); 8 string strsql1 = string.Format("select NewsId from RNews where NewsId='{0}'",Id); 9 if (SqlHelper.Exists(strsql1)) 10 { 11 string title = txtUTitle.Text.Trim(); 12 string strsql2 = string.Format("update RNews set Title= '{0}' where NewsId='{1}'", title, Id); 13 if (SqlHelper.ExecteNonQueryText(strsql2) > 0) 14 { 15 Response.Write("更新成功!"); 16 } 17 BindRnews(); 18 } 19 else 20 { 21 Response.Write("ID不存在!"); 22 } 23 } 24 } 25 catch 26 { 27 Response.Write("系统正在更新,请联系管理员!"); 28 } 29 }
效果: