当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中获取新增加记录的ID Access版

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

雷锋照片大全,牟思澄,有机厨坊网

这里参考了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
此方法还是不错的.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网