跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

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复制代码

网站:

域名:

logo地址:

排序:

是否审核:

是

否

显示方式:

文字

图片

待定

 

ID="btnEnter" runat="server" OnClick="btnEnter_Click" Text="绑定" />

我对代码做了很多注释,大家有兴趣可以看看:

复制代码代码如下:

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

{

/// 

/// dataHandle 的摘要说明

/// 

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);

/// 

/// 建立文本框到数据字段的绑定

/// 

public void AddBind(TextBox tbx, string field, bool isStringType)

{

alBinderItems.Add(new BinderItem(tbx, field, isStringType));

}

/// 

/// 下拉列表

/// 

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));

}

/// 

/// 多选框

/// 

public void AddBind(CheckBoxList cb, string field, bool isStringType)

{

alBinderItems.Add(new BinderItem(cb, field, isStringType));

}

/// 

/// 需要修改数据时取出数据库中的记录填充到表单中

/// 

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 = this.GetDataTable();

//如果没有记录则抛出异常

if (dt.Rows.Count == 0)

{

throw new ArgumentException("记录不存在");

}

DataRow dr = dt.Rows[0];

for (int j = 0; j < alBinderItems.Count; j++)

{

bi = (BinderItem)alBinderItems[j];

bi.SetValue(dr[bi.field].ToString());

}

}

/// 

/// 该方法实现从alBinderItems到alFieldItems的转换,目的:alFieldItems可以转为DbKeyItem,操作数据库时需要用到DbKeyItem

/// 

public void Add()

{

if (this.alBinderItems.Count == 0)

{

return;

}

BinderItem bi = null;

for (int i = 0; i < alBinderItems.Count; i++)

{

bi = ((BinderItem)alBinderItems[i]);

AddFieldItem(bi.field, bi.GetValue());

}

}

/// 

/// 添加一个字段/值对到数组中

/// 

public void AddFieldItem(string _fieldName, object _fieldValue)

{

_fieldName = "[" + _fieldName + "]";

//遍历看是否已经存在字段名

for (int i = 0; i < this.alFieldItems.Count; i++)

{

if (((DbKeyItem)this.alFieldItems[i]).fieldName == _fieldName)

{

throw new ArgumentException("字段已经存在");

}

}

this.alFieldItems.Add(new DbKeyItem(_fieldName, _fieldValue));

}

#endregion

#region 操作数据

#region 这里声明有关数据操作的必要参数

//当前所使用的数据库连接

protected OleDbConnection conn;

//当前所使用的命令对象

protected OleDbCommand cmd = new OleDbCommand();

//当前所使用的数据库适配器

protected OleDbDataAdapter da;

//当前的SQL语句

public string sqlCmd = string.Empty;

//当前操作所涉及的数据库表名

public string tableName = string.Empty;

//SQL条件

public string ConditionExpress;

//用于存放从数据库中取得的数据记录

protected DataTable dt;

#endregion

/// 

/// 根据当前alFieldItem数组中存储的字段/值向指定表中添加一条记录。返回自动增长id

/// 

/// 

/// 

public int InsertData(string _talbeName)

{

this.tableName = _talbeName;

this.sqlCmd = "insert into " + this.tableName + "(";

string temValue = " values(";

for (int i = 0; i < this.alFieldItems.Count; i++)

{

this.sqlCmd += ((DbKeyItem)alFieldItems[i]).fieldName + ",";

temValue += "@para" + i.ToString() + ",";

}

//分别去掉,

this.sqlCmd = Input.CutComma(this.sqlCmd) + ")" + Input.CutComma(temValue) + ")";

//声明执行语句

this.cmd.CommandText = this.sqlCmd;

GenParameters();

cmd.ExecuteNonQuery();

int autoId = 0;

try

{

cmd.CommandText = "select @@identity as id";

autoId = Convert.ToInt32(cmd.ExecuteScalar());

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

return autoId;

}

/// 

/// 根据当前alFieldItem数组中存储的字段/值和条件表达式所指定的条件来更新数据库中的记录,返回受影响的行数

/// 

/// 更新的数据表名称

/// 返回此次操作所影响的数据行数

public int UpData(string _tableName)

{

this.tableName = _tableName;

this.sqlCmd = "update " + this.tableName + " set ";

for (int i = 0; i < this.alFieldItems.Count; i++)

{

this.sqlCmd += ((DbKeyItem)alFieldItems[i]).fieldName;

this.sqlCmd += "=";

this.sqlCmd += "@para";

this.sqlCmd += i.ToString();

this.sqlCmd += ",";

}

this.sqlCmd = Input.CutComma(this.sqlCmd);

if (this.ConditionExpress != string.Empty)

{

this.sqlCmd = this.sqlCmd + " where " + this.ConditionExpress;

}

this.cmd.CommandText = this.sqlCmd;

this.GenParameters();

int effectedLines = this.cmd.ExecuteNonQuery();

return effectedLines;

}

/// 返回查询结果DataTable

public DataTable GetDataTable()

{

DataSet ds = this.GetDataSet();

return ds.Tables[0];

}

/// 

/// 根据当前指定的SqlCmd获取DataSet,如果条件表达式不为空则会被清空,

/// 所以条件表达式必须包含在SqlCmd中

/// 

public DataSet GetDataSet()

{

this.ConditionExpress = string.Empty;

this.cmd.CommandText = this.sqlCmd;

this.GenParameters();

DataSet ds = new DataSet();

this.da.SelectCommand = this.cmd;

this.da.Fill(ds);

return ds;

}

/// 

/// 产生OleDbCommand对象所需的参数

/// 

/// 

protected void GenParameters()

{

if (this.alFieldItems.Count > 0)

{

for (int i = 0; i < this.alFieldItems.Count; i++)

{

cmd.Parameters.AddWithValue("@para" + i.ToString(), ((DbKeyItem)alFieldItems[i]).fieldValue.ToString());

}

}

}

#endregion

}

public class BinderItem

{

//每个绑定控件都以object的形式被存储的

public object obj;

//绑定到数据库的字段名称

public string field;

//是否是字符串类型

public bool isStringType;

/// 

/// 构造函数

/// 

/// 需要绑定的控件对象

/// 绑定到的数据表字段名称

/// 是否是字符串类型

public BinderItem(object _obj, string _field, bool _isStringType)

{

this.obj = _obj;

this.field = _field;

this.isStringType = _isStringType;

}

/// 

/// 根据控件类型获得控件的值

/// 

/// 

public string GetValue()

{

//字符串类型

if (obj is String)

{

return (string)obj;

}

//下拉框

if (obj is DropDownList)

{

DropDownList dd = (DropDownList)obj;

return dd.SelectedValue;

}

//多选框

if (obj is CheckBoxList)

{

string s = string.Empty;

CheckBoxList cb = (CheckBoxList)obj;

for (int i = 0; i < cb.Items.Count; i++)

{

if (cb.Items[i].Selected)

{

s += cb.Items[i].Value + ",";

}

}

return s;

}

//文本框

if (obj is TextBox)

{

TextBox tbx = (TextBox)obj;

return tbx.Text.Trim();

}

//Label

if (obj is Label)

{

Label lbl = (Label)obj;

return lbl.Text;

}

//单选组

if (obj is RadioButtonList)

{

RadioButtonList rb = (RadioButtonList)obj;

return rb.SelectedValue;

}

return string.Empty;

}

/// 

/// 根据控件类型设定控件的值

/// 

/// 要设定的值

public void SetValue(string _value)

{

//字符串类型

if (obj is string)

{

string s = (string)obj;

s = _value;

return;

}

//文本框

if (obj is TextBox)

{

TextBox tbx = (TextBox)obj;

tbx.Text = _value;

return;

}

//单选按钮

if (obj is RadioButtonList)

{

RadioButtonList rb = (RadioButtonList)obj;

rb.SelectedValue = _value;

return;

}

//下拉列表

if (obj is DropDownList)

{

DropDownList dd = (DropDownList)obj;

dd.SelectedValue = _value;

return;

}

}

}

/// 

/// 数据表中的字段属性:字段名,字段值

/// 

public class DbKeyItem

{

/// 

/// 字段名称

/// 

public string fieldName;

/// 

/// 字段值

/// 

public string fieldValue;

public DbKeyItem(string _fileName, object _fieldValue)

{

this.fieldName = _fileName;

this.fieldValue = _fieldValue.ToString();

}

}

}

return;

}

//单选按钮

if (obj is RadioButtonList)

{

RadioButtonList rb = (RadioButtonList)obj;

rb.SelectedValue = _value;

return;

}

//下拉列表

if (obj is DropDownList)

{

DropDownList dd = (DropDownList)obj;

dd.SelectedValue = _value;

return;

}

}

}

/// 

/// 数据表中的字段属性:字段名,字段值

/// 

public class DbKeyItem

{

/// 

/// 字段名称

/// 

public string fieldName;

/// 

/// 字段值

/// 

public string fieldValue;

public DbKeyItem(string _fileName, object _fieldValue)

{

this.fieldName = _fileName;

this.fieldValue = _fieldValue.ToString();

}

}

}

您可能感兴趣的文章:ZKEACMS for .Net Core深度解析asp.net的cms 核心代码篇asp.net的cms 原理篇asp.net的cms 绑定数据篇CentOS上运行ZKEACMS的详细过程

相关文章