欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

相關(guān)文章

  • NET?NativeAOT?用法指南

    NET?NativeAOT?用法指南

    NativeAOT?是?.NET?中一個非常棒和強大的工具,有了?NativeAOT,你可以以可預(yù)測的性能構(gòu)建你的應(yīng)用,同時節(jié)省資源(更低的內(nèi)存占用和更小的二進制大?。?本文介紹NET?NativeAOT?指南,感興趣的朋友一起看看吧
    2024-02-02
  • asp.net下生成99個不同的隨機數(shù)

    asp.net下生成99個不同的隨機數(shù)

    asp.net下生成99個不同的隨機數(shù)...
    2007-04-04
  • .NET開發(fā)基礎(chǔ):從簡單的例子理解泛型 分享

    .NET開發(fā)基礎(chǔ):從簡單的例子理解泛型 分享

    .Net開發(fā)基礎(chǔ)系列文章,對自己之前寫過的代碼備忘,如能給人予幫助,不甚榮幸。個人能力有限,如有差錯或不足,請及時指正。
    2013-06-06
  • ASP.NET Core3.x API版本控制的實現(xiàn)

    ASP.NET Core3.x API版本控制的實現(xiàn)

    這篇文章主要介紹了ASP.NET Core3.x API版本控制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • asp.net core 使用 TestServer 來做集成測試的方法

    asp.net core 使用 TestServer 來做集成測試的方法

    這篇文章主要介紹了asp.net core 使用 TestServer 來做集成測試,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • .NET中方法的注意事項總結(jié)

    .NET中方法的注意事項總結(jié)

    這篇文章主要介紹了.NET中方法的注意事項,較為詳細的分析了.NET中方法中較為常見的技巧、方法與注意事項,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • .NET實現(xiàn)熱插拔功能(動態(tài)替換功用)方案實例

    .NET實現(xiàn)熱插拔功能(動態(tài)替換功用)方案實例

    如果某個"功能"需要動態(tài)更新?這種動態(tài)更新,可能是需求驅(qū)動的,也可能是為了修改 BUG,面對這種場景,如何實現(xiàn)“熱插拔”呢?先解釋一下“熱插拔”:在系統(tǒng)運行過程動態(tài)替換某些功能,不用重啟系統(tǒng)進程。下面看例子
    2013-11-11
  • ASP.NET Core擴展庫的相關(guān)功能介紹

    ASP.NET Core擴展庫的相關(guān)功能介紹

    這篇文章主要介紹了ASP.NET Core擴展庫的相關(guān)功能,幫助大家更好的理解和學習使用.Net技術(shù),感興趣的朋友可以了解下
    2021-03-03
  • aspx如果引用cs中的變量的方法

    aspx如果引用cs中的變量的方法

    引用cs中變量的實現(xiàn)方法
    2008-07-07
  • ASP.NET?MVC使用Session會話保持表單狀態(tài)

    ASP.NET?MVC使用Session會話保持表單狀態(tài)

    這篇文章介紹了ASP.NET?MVC使用Session會話保持表單狀態(tài)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09

最新評論