asp.net to get new added records ID Access version _ Practical tips

Source: Internet
Author: User
Tags html tags
This refers to the code in Erist.protal.
Copy Code code 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</returns> of newly added articles
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;
formatting HTML tags
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";
Get Next ID number
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 Add an Introduction to the article Properties Jiangyong
OLECMD.PARAMETERS.ADD ("Articleintroduction", articleintroduction);
Perform
Erist.Common.Data.DataProvider.ExecNonQueryOle (Olecmd);
return ArticleID;
}

Notice that the background color is orange where the Erist.Common.Data.DataProvider.GetAutoID is called ()
The following is the code for this method
Copy Code code as follows:

/**////<summary>
Get Data Set
</summary>
<param name= "SQLCMD" > Execute command sqlcommand</param>
<returns> returns the obtained data set </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)
Captures the data-tier error and returns it to the previous layer.
{
Throw ex;
}
Finally
{
Break link
if (OleCmd.Connection.State = = ConnectionState.Open) OleCmd.Connection.Close ();

}
}

/**////<summary>
Get the maximum field value for 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;
}
}

According to the FieldName and TableName to read the database the current largest fieldname if not returned 1
On the basis of this +1 because you want to get the only fieldname to add a record
Here fieldname is ArticleID
This method is still good.
Related Article

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.