asp.net access添加返回自遞增id的實現(xiàn)方法
更新時間:2008年08月07日 23:26:52 作者:
今天花了一點時間研究了這個問題,除此之外,還順帶研究了小孔子cms添加數(shù)據(jù)的過程,access添加返回自遞增id也是從小孔子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;
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初始化該對象,將產(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對象所需的參數(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>
/// 添加一個字段/值對到數(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ù)當前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)
{
//彈出錯誤信息,僅僅是為了幫助調(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ù)庫的簡單方法
- asp.net中獲取新增加記錄的ID Access版
- asp.net訪問Access數(shù)據(jù)庫溢出錯誤
- asp.net(C#) Access 數(shù)據(jù)操作類
- asp.net 數(shù)據(jù)庫備份還原(sqlserver+access)
- asp.net和asp下ACCESS的參數(shù)化查詢
- ACCESS的參數(shù)化查詢,附VBSCRIPT(ASP)和C#(ASP.NET)函數(shù)
- ASP.net(c#)用類的思想實現(xiàn)插入數(shù)據(jù)到ACCESS例子
- ASP.NET 鏈接 Access 數(shù)據(jù)庫路徑問題最終解決方案
- ASP.NET連接 Access數(shù)據(jù)庫的幾種方法
相關(guān)文章
.NET開發(fā)基礎(chǔ):從簡單的例子理解泛型 分享
.Net開發(fā)基礎(chǔ)系列文章,對自己之前寫過的代碼備忘,如能給人予幫助,不甚榮幸。個人能力有限,如有差錯或不足,請及時指正。2013-06-06ASP.NET Core3.x API版本控制的實現(xiàn)
這篇文章主要介紹了ASP.NET Core3.x API版本控制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06asp.net core 使用 TestServer 來做集成測試的方法
這篇文章主要介紹了asp.net core 使用 TestServer 來做集成測試,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11.NET實現(xiàn)熱插拔功能(動態(tài)替換功用)方案實例
如果某個"功能"需要動態(tài)更新?這種動態(tài)更新,可能是需求驅(qū)動的,也可能是為了修改 BUG,面對這種場景,如何實現(xiàn)“熱插拔”呢?先解釋一下“熱插拔”:在系統(tǒng)運行過程動態(tài)替換某些功能,不用重啟系統(tǒng)進程。下面看例子2013-11-11ASP.NET?MVC使用Session會話保持表單狀態(tài)
這篇文章介紹了ASP.NET?MVC使用Session會話保持表單狀態(tài)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09