asp.net access添加返回自遞增id的實(shí)現(xiàn)方法第2/3頁
更新時間:2008年08月07日 23:26:52 作者:
今天花了一點(diǎn)時間研究了這個問題,除此之外,還順帶研究了小孔子cms添加數(shù)據(jù)的過程,access添加返回自遞增id也是從小孔子cms中研究出來的。
聲明一個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());
}
}
}
這個函數(shù)其實(shí)就是為了產(chǎn)生:
this.cmd.Parameters.AddWithValue("@para1", "阿會楠");
this.cmd.Parameters.AddWithValue("@para2","搜索吧");
this.cmd.Parameters.AddWithValue("@para3","http://www.dhdzp.com");
但用它方便多了,不用一個個去手寫。而關(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)
{
//彈出錯誤信息,僅僅是為了幫助調(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í)這個主要是等價于執(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ù)的作用是為了除去最后的一個逗號。代碼如下:
/// <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;
}
您可能感興趣的文章:
- ASP.NET 連接ACCESS數(shù)據(jù)庫的簡單方法
- asp.net中獲取新增加記錄的ID Access版
- asp.net訪問Access數(shù)據(jù)庫溢出錯誤
- asp.net(C#) Access 數(shù)據(jù)操作類
- asp.net 數(shù)據(jù)庫備份還原(sqlserver+access)
- asp.net和asp下ACCESS的參數(shù)化查詢
- ACCESS的參數(shù)化查詢,附VBSCRIPT(ASP)和C#(ASP.NET)函數(shù)
- ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
- ASP.NET 鏈接 Access 數(shù)據(jù)庫路徑問題最終解決方案
- ASP.NET連接 Access數(shù)據(jù)庫的幾種方法
相關(guān)文章
ASP.NET MVC API 接口驗(yàn)證的示例代碼
本篇文章主要介紹了ASP.NET MVC API 接口驗(yàn)證的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
Asp.net 取得文件后綴名,保存文件,加入文字水印的代碼類2008-11-11
.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法
這篇文章介紹了.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
.NET 下運(yùn)用策略模式(組合行為和實(shí)體的一種模式)
我簡單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來,并采用組合(Has-a)的方式,來組合行為和實(shí)體的一種模式比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實(shí)現(xiàn)模板2012-12-12
.NET+JS對用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實(shí)例代碼
.NET+JS對用戶輸入內(nèi)容進(jìn)行字?jǐn)?shù)提示功能的實(shí)例代碼,需要的朋友可以參考一下2013-06-06

