asp.net access添加返回自遞增id的實(shí)現(xiàn)方法
更新時(shí)間:2008年08月07日 23:26:52 作者:
今天花了一點(diǎn)時(shí)間研究了這個(gè)問(wèn)題,除此之外,還順帶研究了小孔子cms添加數(shù)據(jù)的過(guò)程,access添加返回自遞增id也是從小孔子cms中研究出來(lái)的。
全部代碼如下,自己看看吧。
復(fù)制代碼 代碼如下:
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初始化該對(duì)象,將產(chǎn)生未處理的“System.NullReferenceException”類型的異常
protected OleDbCommand cmd = new OleDbCommand();
protected string tableName = string.Empty;
protected string fieldName = string.Empty;
protected string sqlText = string.Empty;
/// <summary>
/// 產(chǎn)生OleDbCommand對(duì)象所需的參數(shù)
/// </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>
/// 數(shù)據(jù)表中的字段屬性:字段名,字段值
/// </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>
/// 添加一個(gè)字段/值對(duì)到數(shù)組中
/// </summary>
public void AddFieldItem(string _fieldName, object _fieldValue)
{
_fieldName = "[" + _fieldName + "]";
//遍歷看是否已經(jīng)存在字段名
for (int i = 0; i < this.alFieldItems.Count; i++)
{
if (((DbKeyItem)this.alFieldItems[i]).fieldName == _fieldName)
{
throw new ArgumentException("字段已經(jīng)存在");
}
}
this.alFieldItems.Add(new DbKeyItem(_fieldName, _fieldValue));
}
/// <summary>
/// 根據(jù)當(dāng)前alFieldItem數(shù)組添加一條記錄,并返回添加后的ID
/// </summary>
/// <param name="_tableName">要插入數(shù)據(jù)的表名</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)
{
//彈出錯(cuò)誤信息,僅僅是為了幫助調(diào)試,可以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());
}
}
您可能感興趣的文章:
- ASP.NET 連接ACCESS數(shù)據(jù)庫(kù)的簡(jiǎn)單方法
- asp.net中獲取新增加記錄的ID Access版
- asp.net訪問(wèn)Access數(shù)據(jù)庫(kù)溢出錯(cuò)誤
- asp.net(C#) Access 數(shù)據(jù)操作類
- asp.net 數(shù)據(jù)庫(kù)備份還原(sqlserver+access)
- asp.net和asp下ACCESS的參數(shù)化查詢
- ACCESS的參數(shù)化查詢,附VBSCRIPT(ASP)和C#(ASP.NET)函數(shù)
- ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
- ASP.NET 鏈接 Access 數(shù)據(jù)庫(kù)路徑問(wèn)題最終解決方案
- ASP.NET連接 Access數(shù)據(jù)庫(kù)的幾種方法
相關(guān)文章
ASP.NET MVC API 接口驗(yàn)證的示例代碼
本篇文章主要介紹了ASP.NET MVC API 接口驗(yàn)證的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類2008-11-11
.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法
這篇文章介紹了.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
.NET 下運(yùn)用策略模式(組合行為和實(shí)體的一種模式)
我簡(jiǎn)單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來(lái),并采用組合(Has-a)的方式,來(lái)組合行為和實(shí)體的一種模式比如,.NET中對(duì)數(shù)組排序的Sort的方法就是一個(gè)策略模式的實(shí)現(xiàn)模板2012-12-12
.NET+JS對(duì)用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實(shí)例代碼
.NET+JS對(duì)用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實(shí)例代碼,需要的朋友可以參考一下2013-06-06
如何ASP.NET Core Razor中處理Ajax請(qǐng)求
本篇技術(shù)文章主要給大家講述了如何ASP.NET Core Razor中處理Ajax請(qǐng)求這方面的知識(shí)點(diǎn),有興趣的朋友參考下。2018-01-01

