当前位置:编程学习 > C#/ASP.NET >>

asp.net小孔子cms中的数据添加修改

答案:题外话:我为什么研究小孔子的cms,从我自己写一个cms我就开始研究起别人的cms,早期我是研究netcms,但这系统过于庞大,看上去十分的累,也没那个精力,于是打算从一套比较小的开始研究,于是小孔子cms就进入了我的研究范围。没过多久我就放下我手中的cms,决定研究清楚有了更多经验再继续写完我没有完成的cms。

最近都在看小孔子cms的代码,其添加与修改数据十分方便,做下笔记,代码主要提取自小孔子cms,去掉了不用的函数并把相关代码写到一个文件中:

结合上面的图片,当我们要往数据库中添加数据时,代码如下:

dataHandle doh = new dataHandle();
doh.AddBind(tbxWebName, "link_webname", true);
doh.AddBind(tbxWebUrl, "link_weburl", true);
doh.AddBind(tbxLogoUrl, "link_logourl", true);
doh.AddBind(tbxOrderNum, "link_ordernum", false);
doh.AddBind(ddlStyle, "link_style", false);
doh.AddBind(rblAudit, "link_audit", false);
doh.Add();
int result = Convert.ToInt32(doh.InsertData("db_link"));
Response.Write(result.ToString());
绑定数据指的是从数据库中读取一条记录,并自动绑定到表单的控件中,代码如下(假设读取的id=8):
复制代码 代码如下:


dataHandle doh = new dataHandle();
doh.AddBind(tbxWebName, "link_webname", true);
doh.AddBind(tbxWebUrl, "link_weburl", true);
doh.AddBind(tbxLogoUrl, "link_logourl", true);
doh.AddBind(tbxOrderNum, "link_ordernum", false);
doh.AddBind(ddlStyle, "link_style", false);
doh.AddBind(rblAudit, "link_audit", false);
doh.ConditionExpress = "id = 8";
doh.tableName = "db_link";
doh.BindWhenUp();

修改数据与添加数据差不多:
复制代码 代码如下:

dataHandle doh = new dataHandle();
doh.ConditionExpress = "id = 8";
doh.AddBind(tbxWebName, "link_webname", true);
doh.AddBind(tbxWebUrl, "link_weburl", true);
doh.AddBind(tbxLogoUrl, "link_logourl", true);
doh.AddBind(tbxOrderNum, "link_ordernum", false);
doh.AddBind(ddlStyle, "link_style", false);
doh.AddBind(rblAudit, "link_audit", false);
doh.Add();
int result = Convert.ToInt32(doh.UpData("db_link"));
Response.Write(result);

而aspx文件详细代码: XML/HTML复制代码
网站:
<asp:TextBox ID="tbxWebName" runat="server"></asp:TextBox>
<br />
<br />
域名:<asp:TextBox ID="tbxWebUrl" runat="server"></asp:TextBox><br />
<br />
logo地址:<asp:TextBox ID="tbxLogoUrl" runat="server" Width="198px"></asp:TextBox><br />
<br />
排序:<asp:TextBox ID="tbxOrderNum" runat="server"></asp:TextBox><br />
<br />
是否审核:<asp:RadioButtonList ID="rblAudit" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1">是</asp:ListItem>
<asp:ListItem Selected="True" Value="0">否</asp:ListItem>
</asp:RadioButtonList>
<br />
<br />
显示方式:
<br />
<asp:DropDownList ID="ddlStyle" runat="server">
<asp:ListItem Value="1">文字</asp:ListItem>
<asp:ListItem Value="2">图片</asp:ListItem>
<asp:ListItem Value="3">待定</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:Button ID="btnOk" runat="server" Text="提交" OnClick="btnOk_Click" /> <asp:Button
ID="btnEnter" runat="server" OnClick="btnEnter_Click" Text="绑定" />
<asp:Button ID="btnUp" runat="server" OnClick="btnUp_Click" Text="更改" /><br />
<br />
<asp:Label ID="lblResult" runat="server" Text="结果"></asp:Label></div>
我对代码做了很多注释,大家有兴趣可以看看:
复制代码 代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;

namespace mycms.DataOper.Data
{
/// <summary>
/// dataHandle 的摘要说明
/// </summary>
public class dataHandle
{
public dataHandle()
{
this.conn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = |DataDirectory|mycms.mdb");
this.conn.Open();
this.cmd = conn.CreateCommand();
this.da = new OleDbDataAdapter();
}

#region webform
//这个用来存放包括控件类型,字段,是否是字符串
public ArrayList alBinderItems = new ArrayList(8);

//这个只用来存放字段,值
public ArrayList alFieldItems = new ArrayList(8);

/// <summary>
/// 建立文本框到数据字段的绑定
/// </summary>
public void AddBind(TextBox tbx, string field, bool isStringType)
{
alBinderItems.Add(new BinderItem(tbx, field, isStringType));
}

/// <summary>
/// 下拉列表
/// </summary>
public void AddBind(DropDownList dd, string field, bool isStringType)
{
alBinderItems.Add(new BinderItem(dd, field, isStringType));
}

public void AddBind(RadioButtonList rb, string field, bool isStringType)
{
alBinderItems.Add(new BinderItem(rb, field, isStringType));
}

/// <summary>
/// 多选框
/// </summary>
public void AddBind(CheckBoxList cb, string field, bool isStringType)
{
alBinderItems.Add(new BinderItem(cb, field, isStringType));
}

/// <summary>
/// 需要修改数据时取出数据库中的记录填充到表单中
/// </summary>
public void BindWhenUp()
{
if (alBinderItems.Count == 0)
{
return;
}
BinderItem bi;


StringBuilder sbSql = new StringBuilder("select ");
for (int i = 0; i < alBinderItems.Count; i++)
{
bi = (BinderItem)alBinderItems[i];
//防止出现变量名
sbSql.Append("[" + bi.field + "]");
sbSql.Append(",");
}
sbSql.Remove(sbSql.Length - 1,1);
sbSql.Append(" from ");
sbSql.Append(this.tableName);
sbSql.Append(" where 1 = 1 and ");
sbSql.Append(this.ConditionExpress);

this.sqlCmd = sbSql.ToString();
dt

上一个:asp.net高效替换大容量字符实现代码
下一个:asp.net SqlHelper数据访问层的使用

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,