Obtain the ID Access version of the newly added record in asp.net.

Source: Internet
Author: User

The code in Erist. Protal is referenced here. Copy codeThe Code is as follows: // <summary>
/// Add a new article
/// </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 of the newly added article </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;
// Format the HTML Tag
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 ";
// Obtain the next 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 );
// Add the description attribute Jiang Yong to the article
OleCmd. Parameters. Add ("ArticleIntroduction", ArticleIntroduction );
// Execute
Erist. Common. Data. DataProvider. ExecNonQueryOle (OleCmd );
Return ArticleID;
}

Note that Erist. Common. Data. DataProvider. GetAutoID () is called when the background color is orange ()
The code for this method is as follows:Copy codeThe Code is as follows:/** // <summary>
/// Obtain the dataset
/// </Summary>
/// <Param name = "SqlCmd"> SqlCommand for executing commands </param>
/// <Returns> return the obtained dataset </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)
// Capture the data layer error and return it to the previous layer.
{
Throw ex;
}
Finally
{
// Disconnect
If (OleCmd. Connection. State = ConnectionState. Open) OleCmd. Connection. Close ();

}
}

/** // <Summary>
/// Obtain the maximum field value of a table
/// </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;
}
}

Read the largest FieldName in the database based on the passed FieldName and TableName. If 1 is not returned
If yes, + 1 is the only FieldName to be added.
Here FieldName is ArticleID
This method is good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.