asp.net access添加返回自递增id的实现方法第2/3页

 更新时间:2008年08月07日 23:26:52   作者:  
今天花了一点时间研究了这个问题,除此之外,还顺带研究了小孔子cms添加数据的过程,access添加返回自递增id也是从小孔子cms中研究出来的。

声明一个ArrayList类,并通过AddFieldItem方法可以将字段名,字段值添加进ArrayList。
复制代码 代码如下:

/// <summary>    
/// 产生OleDbCommand对象所需的参数    
/// </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());    
        }    
    }    
}   

这个函数其实就是为了产生: 
this.cmd.Parameters.AddWithValue("@para1", "阿会楠");     
this.cmd.Parameters.AddWithValue("@para2","搜索吧");     
this.cmd.Parameters.AddWithValue("@para3","https://www.jb51.net");    
但用它方便多了,不用一个个去手写。而关键的函数: 
折叠展开
/// <summary>     
/// 根据当前alFieldItem数组添加一条记录,并返回添加后的ID     
/// </summary>     
/// <param name="_tableName">要插入数据的表名</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)     
    {     
        //弹出错误信息,仅仅是为了帮助调试,可以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;     

}    
其实这个主要是等价于执行: 
SQL复制代码 
insert into db_news([news_Title],[news_Source],[news_Anthor]) values(@para0,@para1,@para2)      
//产生所要的参数     
this.GenParameters();     
select @@identity as id    
而CutComma函数的作用是为了除去最后的一个逗号。代码如下: 

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

相关文章

最新评论