asp.net中获取新增加记录的ID Access版

时间:2021-11-23 16:21:16

这里参考了Erist.Protal里的代码 

复制代码代码如下:


/// <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标记 
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"; 
//取得下一个ID号 
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将文章增加简介属性 姜勇 
OleCmd.Parameters.Add("ArticleIntroduction",ArticleIntroduction); 
//执行 
Erist.Common.Data.DataProvider.ExecNonQueryOle(OleCmd); 
return ArticleID; 


注意看背景色为橙色的地方调用了Erist.Common.Data.DataProvider.GetAutoID() 
下面是此方法的代码 

复制代码代码如下:


/**//// <summary> 
/// 取得数据集 
/// </summary> 
/// <param name="SqlCmd">执行命令的SqlCommand</param> 
/// <returns>返回取得的数据集</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 ) 
//捕获数据层错误并返回给上一层。 

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; 


根据传过来的FieldName 和TableName 来读数据库当前最大的FieldName 如果没有返回1 
有则在此基础上+1 因为是要获取要添加记录的唯一FieldName 
此处FieldName是ArticleID 
此方法还是不错的.