学了这样长时间的C#却完全没弄懂面向对象这块,在这里在.net的开发中我们常常会很多的时候会反复的去连接数据库连接,但对于想在技术方面有所提升我们不能老这样做,要想方法去解决掉,所以oo思想在我们的开发中真的很给力,对于想完全弄懂确实要花点功夫,笔者在这里也只是个菜鸟,所以在这里也不多说了。直接在这里上代码了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
/// <summary>
///SQLhelp 的摘要说明
/// </summary>
public class SQLhelp
{
SqlConnection coon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
public SQLhelp()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//数据库的查询
public DataSet data_select(string sql)
{
coon.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql,coon);
da.Fill(ds);
coon.Close();
return ds;
}
//执行数据库的方法
public int Exec(string sql)
{
coon.Open();
SqlTransaction tran = coon.BeginTransaction();
try
{
SqlCommand com = new SqlCommand(sql, coon, tran);
int i = com.ExecuteNonQuery();
tran.Commit();
return i;
}
catch
{
return 0;
tran.Rollback();
}
finally
{
coon.Close();
}
}
//数据读取的方法
public SqlDataReader Read(string sql)
{
coon.Open();
SqlCommand com = new SqlCommand(sql,coon);
SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
//数据绑定
public void GriveBind(GridView grid, DataSet ds)
{
grid.DataSource = ds;
grid.DataBind();
}
/// <summary>
/// GridView和datalist控件绑定数据加分页
/// </summary>
/// <param name="grid">GridView控件ID</param>
/// <param name="ds">datalist控件ID</param>
/// <param name="count">当前页数</param>
/// <param name="page">每页显示数据条数</param>
public int Data_bind(GridView grid, DataSet ds,int count,int page)
{
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;
ps.PageSize = page;
ps.CurrentPageIndex = count - 1;
return ps.PageCount;
grid.DataSource = ps;
grid.DataBind(); }
//DropDownList绑定
public void dropDown_bind(DropDownList drop,DataSet ds,string Name,string ID)
{
drop.DataSource = ds;
drop.DataTextField = Name;
drop.DataValueField = ID;
drop.DataBind();
}
}
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Data;
6 using System.Data.SqlClient;
7 using System.Configuration;
8 using System.Web.UI.WebControls;
9
10 /// <summary>
11 ///SQLhelp 的摘要说明
12 /// </summary>
13 public class SQLhelp
14 {
15
16
17 SqlConnection coon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
18 public SQLhelp()
19 {
20 //
21 //TODO: 在此处添加构造函数逻辑
22 //
23
24
25 }
26 //数据库的查询
27 public DataSet data_select(string sql)
28 {
29 coon.Open();
30 DataSet ds = new DataSet();
31 SqlDataAdapter da = new SqlDataAdapter(sql,coon);
32 da.Fill(ds);
33 coon.Close();
34 return ds;
35 }
36 //执行数据库的方法
37 public int Exec(string sql)
38 {
39 coon.Open();
40 SqlTransaction tran = coon.BeginTransaction();
41 try
42 {
43 SqlCommand com = new SqlCommand(sql, coon, tran);
44 int i = com.ExecuteNonQuery();
45 tran.Commit();
46 return i;
47
48 }
49 catch
50 {
51 return 0;
52 tran.Rollback();
53
54 }
55
56 finally
57 {
58
59
60 coon.Close();
61
62 }
63 }
64 //数据读取的方法
65 public SqlDataReader Read(string sql)
66 {
67
68 coon.Open();
69 SqlCommand com = new SqlCommand(sql,coon);
70 SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
71 return dr;
72
73 }
74 //数据绑定
75 public void GriveBind(GridView grid, DataSet ds)
76 {
77
78 grid.DataSource = ds;
79 grid.DataBind();
80
81 }
82 /// <summary>/// GridView和datalist控件绑定数据加分页 /// </summary>/// <param name="grid">GridView控件ID</param>/// <param name="ds">datalist控件ID</param>/// <param name="count">当前页数</param>/// <param name="page">每页显示数据条数</param> public int Data_bind(GridView grid, DataSet ds,int count,int page) { PagedDataSource ps = new PagedDataSource(); ps.DataSource = ds.Tables[0].DefaultView; ps.AllowPaging = true; ps.PageSize = page; ps.CurrentPageIndex = count - 1; return ps.PageCount; grid.DataSource = ps; grid.DataBind(); } //DropDownList绑定public void dropDown_bind(DropDownList drop,DataSet ds,string Name,string ID) { drop.DataSource = ds; drop.DataTextField = Name; drop.DataValueField = ID; drop.DataBind(); }}
在这里我把常用的数据绑定的方法也放在这个类里面,如果你觉得不习惯的,你可以单独下个DataBind类来封装这些数据绑定控件的方法,个人也看过一些sqlhelp的类,但感觉很多东西很生僻,在这里讲我们常用的,反复用到的写在一个类里面。如果有朋友还有啥好的想法,也可以留言,再次声明这个类很适合新手朋友,有老鸟也不要喷。在这我也就只显示下GridView的前台绑定,删除很编辑。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Height="88px"
Width="530px" style="margin-bottom: 0px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Adminid" HeaderText="管理员编号" />
<asp:BoundField DataField="Adminname" HeaderText="管理员名称" />
<asp:BoundField DataField="Adminpassword" HeaderText="管理员密码" />
<asp:HyperLinkField DataNavigateUrlFields="Adminid"
DataNavigateUrlFormatString="Default.aspx?Adminid={0}" HeaderText="编辑"
Text="修改" NavigateUrl="~/Default.aspx">
<HeaderStyle HorizontalAlign="Left" />
</asp:HyperLinkField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
OnClientClick='return confirm("确定删除?")'
CommandArgument='<%# Eval("Adminid") %>'>删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</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;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default3 : System.Web.UI.Page
{
SQLhelp help = new SQLhelp();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dataBind();
}
}
private void dataBind()
{
string sql = "select * from Admin";
DataSet ds = help.data_select(sql);
help.GriveBind(GridView1,ds);
}
public void LinkButton1_Click(object sender, EventArgs e)
{
string sqls = "delete from Admin where Adminid=" + ((LinkButton)sender).CommandName.ToString();
SQLhelp help = new SQLhelp();
int i = help.Exec(sqls);
if (i>0)
{
Response.Write("<script>alert('删除成功')</script>");
dataBind();
}
else
{
Response.Write("<script>alert('删除失败')</script>");
}
}
}
至于这里编辑的代码:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Data;
8 using System.Data.SqlClient;
9 using System.Configuration;
10
11
12 public partial class _Default : System.Web.UI.Page
13 {
14 SQLhelp help = new SQLhelp();
15 string id ="";
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 id = Request.QueryString["Adminid"];
19 if (!IsPostBack)
20 {
21 if (id!=null)
22 {
23
24
25 string sql = "select * from Admin where Adminid=" + id;
26 DataSet ds = help.data_select(sql);
27 t_username.Text = ds.Tables[0].Rows[0][1].ToString();
28 t_password.Text = ds.Tables[0].Rows[0][2].ToString();
29
30
31 }
32 }
33
34
35 }
36 protected void btn_add_Click(object sender, EventArgs e)
37 {
38
39 string sql = "update Admin set Adminname='"+t_username.Text.Trim()+"',Adminpassword='"+t_password.Text.Trim()+"' where Adminid="+id;
40
41 int i = help.Exec(sql);
42
43 if (i > 0)
44 {
45
46 Response.Write("<script>alert('修改成功')</script>");
47 Response.Redirect("Default3.aspx");
48
49 }
50 else
51 {
52
53 Response.Write("<script>alert('修改失败')</script>");
54
55
56 }
57
58 }
59 }
这里只是自己的简单理解,所以对于其他控件也一样的,大家有好的方法也可以分享给我,这里的分页没做,大家也可以自己手写,也可以去找分页控件。希望能帮到新手朋友。在这里就不多说了,其实大家在写mvc的时候也会用到sqlhelp或其他数据库的,所以在这里的新手朋友也可以去用用,网上的sqlhelp也很多,大家也可以去参考下,找到自己适合的。
发布反链的网站平台权重不高 在选择发布反链的平台网站时,考察该网站的权重也是很重要的。假如在一个本身权重就不高的网站上发布反链,那么效果也是很委曲的。另外值得留意的是,站长朋友们常常做的论坛签名反链也属于低权重的,大量的论坛签名反链也会导致百度反链数目的不不乱。
三、所发的外链平台遭百度惩罚 这点对于发外链的朋友们可能说是最冤枉的了,平白的遭受牵连。所以说,重点仍是在于我们选择发布外链的平台上,选择高质量的、不乱的、权势巨子的外链平台长短常枢纽的。
在大致知晓反链减少的原因之后,我们就可以对症下药了http://www.daydayit.com/

浙公网安备 33010602011771号