asp.net에서 새로 추가된 레코드를 가져오는 ID Access 버전

4508 단어
Erist 참조Protal의 코드
 
  
///
///
///

///
///
///
///
///
///
///
///
///
///
///
///
/// ID
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()
다음은 이 방법의 코드이다
 
  
/**////
///
///

/// SqlCommand
///
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();

}
}

/**////
///
///

///
///
///
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을 읽습니다.
레코드를 추가할 유일한 Field를 얻기 위해 +1을 추가합니다Name
이 FieldName은 ArticleID입니다.
이 방법은 그래도 괜찮다.

좋은 웹페이지 즐겨찾기