最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net access添加返回自遞增id的實(shí)現(xiàn)方法第3/3頁(yè)

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

相關(guān)文章

最新評(píng)論

勐海县| 集安市| 顺义区| 长兴县| 漳浦县| 宝山区| 六安市| 绍兴县| 云南省| 家居| 封开县| 锦州市| 城口县| 阿拉善左旗| 柞水县| 棋牌| 荣昌县| 邯郸县| 苍山县| 潞西市| 龙口市| 伊宁市| 章丘市| 府谷县| 孟津县| 芒康县| 闵行区| 漳州市| 吴川市| 措美县| 曲水县| 若羌县| 梅河口市| 福鼎市| 景德镇市| 佛冈县| 南雄市| 浦县| 武汉市| 武夷山市| 安达市|