asp.net access添加返回自遞增id的實(shí)現(xiàn)方法第2/3頁(yè)
更新時(shí)間:2008年08月07日 23:26:52 作者:
今天花了一點(diǎn)時(shí)間研究了這個(gè)問(wèn)題,除此之外,還順帶研究了小孔子cms添加數(shù)據(jù)的過(guò)程,access添加返回自遞增id也是從小孔子cms中研究出來(lái)的。
聲明一個(gè)ArrayList類(lèi),并通過(guò)AddFieldItem方法可以將字段名,字段值添加進(jìn)ArrayList。
復(fù)制代碼 代碼如下:
/// <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());
}
}
}
這個(gè)函數(shù)其實(shí)就是為了產(chǎn)生:
this.cmd.Parameters.AddWithValue("@para1", "阿會(huì)楠");
this.cmd.Parameters.AddWithValue("@para2","搜索吧");
this.cmd.Parameters.AddWithValue("@para3","http://www.dbjr.com.cn");
但用它方便多了,不用一個(gè)個(gè)去手寫(xiě)。而關(guān)鍵的函數(shù):
折疊展開(kāi)
/// <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;
}
您可能感興趣的文章:
- ASP.NET 連接ACCESS數(shù)據(jù)庫(kù)的簡(jiǎn)單方法
- asp.net中獲取新增加記錄的ID Access版
- asp.net訪問(wèn)Access數(shù)據(jù)庫(kù)溢出錯(cuò)誤
- asp.net(C#) Access 數(shù)據(jù)操作類(lèi)
- asp.net 數(shù)據(jù)庫(kù)備份還原(sqlserver+access)
- asp.net和asp下ACCESS的參數(shù)化查詢(xún)
- ACCESS的參數(shù)化查詢(xún),附VBSCRIPT(ASP)和C#(ASP.NET)函數(shù)
- ASP.net(c#)用類(lèi)的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
- ASP.NET 鏈接 Access 數(shù)據(jù)庫(kù)路徑問(wèn)題最終解決方案
- ASP.NET連接 Access數(shù)據(jù)庫(kù)的幾種方法
相關(guān)文章
asp.net下生成99個(gè)不同的隨機(jī)數(shù)
asp.net下生成99個(gè)不同的隨機(jī)數(shù)...2007-04-04.NET開(kāi)發(fā)基礎(chǔ):從簡(jiǎn)單的例子理解泛型 分享
.Net開(kāi)發(fā)基礎(chǔ)系列文章,對(duì)自己之前寫(xiě)過(guò)的代碼備忘,如能給人予幫助,不甚榮幸。個(gè)人能力有限,如有差錯(cuò)或不足,請(qǐng)及時(shí)指正。2013-06-06ASP.NET Core3.x API版本控制的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core3.x API版本控制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06asp.net core 使用 TestServer 來(lái)做集成測(cè)試的方法
這篇文章主要介紹了asp.net core 使用 TestServer 來(lái)做集成測(cè)試,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11.NET實(shí)現(xiàn)熱插拔功能(動(dòng)態(tài)替換功用)方案實(shí)例
如果某個(gè)"功能"需要?jiǎng)討B(tài)更新?這種動(dòng)態(tài)更新,可能是需求驅(qū)動(dòng)的,也可能是為了修改 BUG,面對(duì)這種場(chǎng)景,如何實(shí)現(xiàn)“熱插拔”呢?先解釋一下“熱插拔”:在系統(tǒng)運(yùn)行過(guò)程動(dòng)態(tài)替換某些功能,不用重啟系統(tǒng)進(jìn)程。下面看例子2013-11-11ASP.NET Core擴(kuò)展庫(kù)的相關(guān)功能介紹
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫(kù)的相關(guān)功能,幫助大家更好的理解和學(xué)習(xí)使用.Net技術(shù),感興趣的朋友可以了解下2021-03-03ASP.NET?MVC使用Session會(huì)話(huà)保持表單狀態(tài)
這篇文章介紹了ASP.NET?MVC使用Session會(huì)話(huà)保持表單狀態(tài)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09