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

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

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

聲明一個(gè)ArrayList類,并通過AddFieldItem方法可以將字段名,字段值添加進(jìn)ArrayList。
復(fù)制代碼 代碼如下:

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

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

}    
其實(shí)這個(gè)主要是等價(jià)于執(zhí)行: 
SQL復(fù)制代碼 
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ù)的作用是為了除去最后的一個(gè)逗號(hào)。代碼如下: 

/// <summary>     
/// 去除字符串最后一個(gè)','號(hào)     
/// </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;     
}    

相關(guān)文章

最新評(píng)論

深水埗区| 黔西| 彭山县| 延庆县| 宜川县| 邹平县| 乌拉特后旗| 基隆市| 湘潭市| 财经| 巧家县| 静安区| 苍溪县| 苏尼特左旗| 唐山市| 四会市| 西盟| 宁波市| 平江县| 兴海县| 商都县| 松滋市| 黄石市| 平武县| 台东市| 罗山县| 莱州市| 山丹县| 通海县| 论坛| 福清市| 永州市| 垫江县| 两当县| 新民市| 大连市| 香格里拉县| 昌平区| 富裕县| 潜山县| 赤城县|