All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET中用哪种方式表格化数据_[Asp.Net教程]
ASP.NET,2.0,中的窗体身份验证_[Asp.Net教程]
在PHP中开发XML应用程序之基础篇_php资料_编程技术
asp.net2.0服务器控件之ImageButton控件
PHP将将,XML,映射为,HTML的代码实例_php资料_编程技术
对比两个DataTable是否相同的正确方法_[Asp.Net教程]
详细学习动态网页制作PHP技术的正则表达式_php资料_编程技术
网页特效:滑动展开与折叠效果_JavaScript技术_编程技术
C#中的try
asp.net2.0网站基础之编写代码
Reading number is top 10 pictures
In the world the most mysterious 21 place landscape4
The wise woman of chest1
The money of more than 100 countries and regions11
星星命名法则
Sell the barbecue as says father du breul3
Summer is most suitable for young people to travel in China1
代沟,真好
我国房地产真相
网络游戏与脑残
迷人的靓女
Download software ranking
Kung fu panda - the secret of the teacher
VC++6.0培训教程
Boxer's Top ten classic battle4
株洲本地在线棋牌游戏
Such love down(擒爱记)
天龙八部十二宫服务端
Boxer's Top ten classic battle7
致我们终将逝去的青春
虚拟机汉化软件
网页特效实例大全
delv published in(发表于) 2014/1/23 3:13:12 Edit(编辑)
VS2005中获取新增记录的ID方法总结_[Asp.Net教程]

VS2005中获取新增记录的ID方法总结_[Asp.Net教程]

VS2005中获取新增记录的ID方法总结_[Asp.Net教程]

1.使用触发器方式


CREATE TRIGGER intrig
ON sales
FOR update AS
declare @before_id (your title_id'type and length),
@after_id (your title_id'type and length)
SELECT @before_id =title_id FROM inserted
SELECT @after_id =title_id FROM deleted
SELECT title_id FROM sales where title_id=@before_id and title_id=@after_id


2.使用ID自增方式



返回最大ID就是新增的ID


3.使用SQL命令语句方式



addAnnouncementCmd.CommandText = "insert into SYS_Announcement (Title) values(@Title) ;";
addAnnouncementCmd.CommandText += "SELECT @ID=@@Identity;";
addAnnouncementCmd.CommandType = CommandType.Text;
addAnnouncementCmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50);


SqlParameter oID = new SqlParameter("@ID", SqlDbType.Int);
oID.Direction = ParameterDirection.Output;
addAnnouncementCmd.Parameters.Add(oID);


try
{
addAnnouncementCmd.Connection.Open();
addAnnouncementCmd.ExecuteNonQuery();
int addID = Convert.ToInt32(addAnnouncementCmd.Parameters["@ID"].Value.ToString());//addID新增的ID
}
finally
{
command.Connection.Close();
}


4. 使用 ado.net 里面获取返回值的方法


--------------------------------------------
假设有存储过程如下:
---------------------------------------------

CREATE proc sp_uptmp @tmpName varchar(50),@srcPos varchar(255)
as

Begin TRAN
insert into t_template values(@tmpName,@srcPos)
COMMIT

return isnull(@@identity,0)
GO


------------------------------------------------------------
在 ado.net 里面获取返回值的方法为(c#):
------------------------------------------------------------
SqlConnection dbconn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("sp_uptmp",dbconn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter tmpName = cmd.Parameters.Add("@tmpName",SqlDbType.VarChar);
SqlParameter srcPos = _cmd.Parameters.Add("@srcPos",SqlDbType.VarChar);
SqlParameter rtnval = cmd.Parameters.Add("rval",SqlDbType.Int);

tmpName.Direction = ParameterDirection.Input;
srcPos.Direction = ParameterDirection.Input;
rtnval.Direction = ParameterDirection.ReturnValue;

tmpName.Value = "";
srcPos.Value = "";
dbconn.Open();
cmd.ExecuteNonQuery();
dbconn.Close();

tmpid = (int)rtnval.Value; //此处即为返回值


5.利用执行查询时,并返回查询所返回的结果集中第一行的第一列方法



只要把ID设为第一列,采用以下方式查询执行就可获得ID


int iKey=(int)cmd.ExecuteScalar();

来源:panyaxiong的blog





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.