asp.net小孔子cms中的数据添加修改
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();
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播
相关文章
- ASP.NET堆和栈四之对托管和非托管资源的垃圾回收和内存分配
- Asp.net GridView使用大全(分页实现)
- asp.net发送邮件实现方法
- asp.net 虚拟主机时常出现MAC验证失败错误之解决方法
- asp.net sql存储过程
- ASP.NET MVC5网站开发之登录、验证和注销管理员篇1(六)
- ASP.NET 2.0中的数据操作之七:使用DropDownList过滤的主/从报表
- ASP.NET使用Global.asax的方法
- ASP.NET 中 Button、LinkButton和ImageButton 三种控件的使用详解
- ASP.NET Maker(代码自动生成工具)v2020.0.4.1 特别安装版(附激活教程)