当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net access添加返回自递增id的实现方法

ASP.NET
AspNetPager与Socut.Data使用方法
asp.net UpdaeProgress的简单用法
asp.net ajaxControlToolkit ValidatorCalloutExtender的简单用法
asp.net 简易生成注册码(数字+大小写字母)
asp.net中利用ashx实现图片防盗链代码
ASP.NET程序中常用代码汇总
ASP.NET 2.0/3.5中直接操作Gridview控件插入新记录
ASP.NET Ajax级联DropDownList实现代码
ASP.NET 2.0写无限级下拉菜单
asp.net Web Services上传和下载文件(完整代码)
asp.net DataGrid控件中弹出详细信息窗口
Asp.NET 多层登陆实现代码
利用Asp.Net回调机制实现进度条
ASP.NET Ref和Out关键字区别分析
Javascript调用Webservice的多种方法
.Net下的签名与混淆图文分析
.Net Compact Framework开发小技巧 推荐
.Net连接Oracle数据库的实现代码
js获取.aspx页面里面的服务器控件和.ascx中的服务器控件值
asp.net下 jquery jason 高效传输数据

ASP.NET 中的 asp.net access添加返回自递增id的实现方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 67 ::
收藏到网摘: n/a

今天花了一点时间研究了这个问题,除此之外,还顺带研究了小孔子cms添加数据的过程,access添加返回自递增id也是从小孔子cms中研究出来的。 先看界面:

添加后数据库:

而所要执行的语句:
复制代码 代码如下:
声明一个ArrayList类,并通过AddFieldItem方法可以将字段名,字段值添加进ArrayList。
复制代码 代码如下:
全部代码如下,自己看看吧。
复制代码 代码如下:

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;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 申明
/// </summary>
protected ArrayList alFieldItems = new ArrayList(10);
//不用new初始化该对象,将产生未处理的“System.NullReferenceException”类型的异常
protected OleDbCommand cmd = new OleDbCommand();
protected string tableName = string.Empty;
protected string fieldName = string.Empty;
protected string sqlText = string.Empty;
/// <summary>
/// 产生OleDbCommand对象所需的参数
/// </summary>
protected void GenParameters()
{
OleDbCommand oleCmd = (OleDbCommand)cmd;
if (this.alFieldItems.Count > 0)
{
for (int i = 0; i < alFieldItems.Count; i++)
{
oleCmd.Parameters.AddWithValue("@para" + i.ToString(),((DbKeyItem)alFieldItems[i]).fieldValue.ToString());
}
}
}
/// <summary>
/// 数据表中的字段属性:字段名,字段值
/// </summary>
public class DbKeyItem
{
/// <summary>
/// 字段名称
/// </summary>
public string fieldName;
/// <summary>
/// 字段值
/// </summary>
public string fieldValue;
public DbKeyItem(string _fieldName, object _fieldValue)
{
this.fieldName = _fieldName;
this.fieldValue = _fieldValue.ToString();
}
}
/// <summary>
/// 添加一个字段/值对到数组中
/// </summary>
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));
}
/// <summary>
/// 根据当前alFieldItem数组添加一条记录,并返回添加后的ID
/// </summary>
/// <param name="_tableName">要插入数据的表名</param>
/// <returns>返回添加后的ID</returns>
public int insert(string _tableName)
{
this.tableName = _tableName;
this.fieldName = string.Empty;
this.sqlText = "insert into " + this.tableName + "(";
string temValue = " values(";
for (int i = 0; i < this.alFieldItems.Count; i++)
{
this.sqlText += ((DbKeyItem)alFieldItems[i]).fieldName + ",";
temValue += "@para" + i.ToString() + ",";
}
//分别去掉,
this.sqlText = Input.CutComma(sqlText) + ")" + Input.CutComma(temValue) + ")" ;
//定义连接字符串
string myString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("App_Data/mycms.mdb");
OleDbConnection conn = new OleDbConnection(myString);
conn.Open();
this.cmd.Connection = conn;
this.cmd.CommandText = this.sqlText;
this.GenParameters();
try
{
this.cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
//弹出错误信息,仅仅是为了帮助调试,可以throw new Exception(ex.Message)
common.salert(ex.Message);
}
int id = 0;
try
{
cmd.CommandText = "select @@identity as id";
id = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception ex)
{
common.salert(ex.Message);
}
conn.Close();
return id;
}
protected void btnOk_Click(object sender, EventArgs e)
{
string name_ = this.tbxUseName.Text.Trim();
string webname_ = this.tbxWebName.Text.Trim();
string url_ = this.tbxUrl.Text.Trim();
AddFieldItem("news_Title", name_);
AddFieldItem("news_Source",webname_);
AddFieldItem("news_Anthor",url_);
common.salert("添加成功,添加后的ID为" + insert("db_news").ToString());
}
}