這裡參考了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
此方法還是不錯的.