這里參考了Erist.Protal里的代碼
復(fù)制代碼 代碼如下:
/// summary>
/// 增加新的文章
/// /summary>
/// param name="ArticleTitle">/param>
/// param name="Author">/param>
/// param name="ArticleFrom">/param>
/// param name="Creator">/param>
/// param name="ModifyBy">/param>
/// param name="Content">/param>
/// param name="ChannelID">/param>
/// param name="IsOnTop">/param>
/// param name="IsCommend">/param>
/// param name="IsCheck">/param>
/// param name="Keyword">/param>
/// param name="ArticleIntroduction">/param>
/// returns>新增加文章的ID/returns>
public int AddArticle(string ArticleTitle,
string Author,
string ArticleFrom,
int Creator,
int ModifyBy,
string Content,
int ChannelID,
bool IsOnTop,
bool IsCommend,
bool IsCheck,
string Keyword,
string ArticleIntroduction)
{
int ArticleID =-1;
//格式化HTML標(biāo)記
ArticleTitle=System.Web.HttpUtility.HtmlEncode(ArticleTitle);
Author=System.Web.HttpUtility.HtmlEncode(Author);
Keyword=System.Web.HttpUtility.HtmlEncode(Keyword);
OleDbConnection OleCon=new OleDbConnection(Globals.ConnectString);
OleDbCommand OleCmd=new OleDbCommand();
OleCmd.CommandType=System.Data.CommandType.StoredProcedure;
OleCmd.Connection=OleCon;
OleCmd.CommandText="AddArticle";
//取得下一個(gè)ID號(hào)
ArticleID= Erist.Common.Data.DataProvider.GetAutoID("ArticleID","Article",Globals.ConnectString);
OleCmd.Parameters.Add("ArticleID",ArticleID);
OleCmd.Parameters.Add("ArticleTitle",ArticleTitle);
OleCmd.Parameters.Add("Author",Author);
OleCmd.Parameters.Add("ArticleFrom",ArticleFrom);
OleCmd.Parameters.Add("Creator",Creator);
OleCmd.Parameters.Add("ModifyBy",ModifyBy);
OleCmd.Parameters.Add("Content",Content);
OleCmd.Parameters.Add("ChannelID",ChannelID);
OleCmd.Parameters.Add("IsOnTop",IsOnTop);
OleCmd.Parameters.Add("IsCommend",IsCommend);
OleCmd.Parameters.Add("IsCheck",IsCheck);
OleCmd.Parameters.Add("Keyword",Keyword);
//2004-2-2將文章增加簡(jiǎn)介屬性 姜勇
OleCmd.Parameters.Add("ArticleIntroduction",ArticleIntroduction);
//執(zhí)行
Erist.Common.Data.DataProvider.ExecNonQueryOle(OleCmd);
return ArticleID;
}
注意看背景色為橙色的地方調(diào)用了Erist.Common.Data.DataProvider.GetAutoID()
下面是此方法的代碼
復(fù)制代碼 代碼如下:
/**//// summary>
/// 取得數(shù)據(jù)集
/// /summary>
/// param name="SqlCmd">執(zhí)行命令的SqlCommand/param>
/// returns>返回取得的數(shù)據(jù)集/returns>
public static DataSet GetDataSetOle(OleDbCommand OleCmd )
{
OleDbDataAdapter t_DataAdapter ;
DataSet t_DataSet=new DataSet();
try
{
if (OleCmd.Connection.State != ConnectionState.Open) OleCmd.Connection.Open();
t_DataAdapter = new OleDbDataAdapter(OleCmd);
t_DataAdapter.Fill(t_DataSet);
return t_DataSet;
}
catch(Exception ex )
//捕獲數(shù)據(jù)層錯(cuò)誤并返回給上一層。
{
throw ex;
}
finally
{
//斷開鏈接
if (OleCmd.Connection.State == ConnectionState.Open) OleCmd.Connection.Close();
}
}
/**//// summary>
/// 取得某一表的最大字段值
/// /summary>
/// param name="FieldName">/param>
/// param name="TableName">/param>
/// returns>/returns>
public static int GetAutoID(string FieldName ,string TableName,string ConnectString)
{
DataSet ds;
OleDbConnection OleCon=new OleDbConnection(ConnectString);
OleDbCommand OleCmd=new OleDbCommand();
OleCmd.CommandText="Select Max(" + FieldName +") as MaxID from " + TableName;
OleCmd.CommandType=System.Data.CommandType.Text;
OleCmd.Connection=OleCon;
ds=GetDataSetOle(OleCmd);
if( ds.Tables[0].Rows[0][0] == DBNull.Value)
{
return 1;
}
else
{
return int.Parse(ds.Tables[0].Rows[0][0].ToString())+ 1;
}
}
根據(jù)傳過來(lái)的FieldName 和TableName 來(lái)讀數(shù)據(jù)庫(kù)當(dāng)前最大的FieldName 如果沒有返回1
有則在此基礎(chǔ)上+1 因?yàn)槭且@取要添加記錄的唯一FieldName
此處FieldName是ArticleID
此方法還是不錯(cuò)的.
您可能感興趣的文章:- ASP.NET 連接ACCESS數(shù)據(jù)庫(kù)的簡(jiǎn)單方法
- asp.net訪問Access數(shù)據(jù)庫(kù)溢出錯(cuò)誤
- asp.net(C#) Access 數(shù)據(jù)操作類
- asp.net 數(shù)據(jù)庫(kù)備份還原(sqlserver+access)
- asp.net access添加返回自遞增id的實(shí)現(xiàn)方法
- 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ù)庫(kù)路徑問題最終解決方案
- ASP.NET連接 Access數(shù)據(jù)庫(kù)的幾種方法