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

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

 更新時間:2008年08月07日 23:26:52   作者:  
今天花了一點時間研究了這個問題,除此之外,還順帶研究了小孔子cms添加數(shù)據(jù)的過程,access添加返回自遞增id也是從小孔子cms中研究出來的。

聲明一個ArrayList類,并通過AddFieldItem方法可以將字段名,字段值添加進ArrayList。
復制代碼 代碼如下:

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

這個函數(shù)其實就是為了產(chǎn)生: 
this.cmd.Parameters.AddWithValue("@para1", "阿會楠");     
this.cmd.Parameters.AddWithValue("@para2","搜索吧");     
this.cmd.Parameters.AddWithValue("@para3","http://www.fzitv.net");    
但用它方便多了,不用一個個去手寫。而關鍵的函數(shù): 
折疊展開
/// <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;     

}    
其實這個主要是等價于執(zhí)行: 
SQL復制代碼 
insert into db_news([news_Title],[news_Source],[news_Anthor]) values(@para0,@para1,@para2)      
//產(chǎn)生所要的參數(shù)     
this.GenParameters();     
select @@identity as id    
而CutComma函數(shù)的作用是為了除去最后的一個逗號。代碼如下: 

/// <summary>     
/// 去除字符串最后一個','號     
/// </summary>     
/// <param name="chr">:要做處理的字符串</param>     
/// <returns>返回已處理的字符串</returns>     
public static string CutComma(string Input)     
{     
    return CutComma(Input, ",");     
}     

public static string CutComma(string Input, string indexStr)     
{     
    if (Input.IndexOf(indexStr) >= 0)     
        return Input.Remove(Input.LastIndexOf(indexStr));     
    else    
        return Input;     
}    

相關文章

最新評論

阿巴嘎旗| 南川市| 西平县| 安福县| 怀集县| 洪江市| 治县。| 高要市| 上虞市| 鄂尔多斯市| 台州市| 安泽县| 兰坪| 龙陵县| 祥云县| 靖边县| 台江县| 惠东县| 长治县| 米易县| 沂源县| 武川县| 牟定县| 上饶县| 黄大仙区| 沙坪坝区| 盱眙县| 平南县| 乌审旗| 阳新县| 同德县| 金山区| 罗田县| 富川| 桑植县| 武威市| 池州市| 南江县| 灌云县| 汕头市| 白朗县|