csharp: using OleDb Getting the identity of the most recently added record

/// <summary>
        /// 执行SQL语句,返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.OleDb.OleDbException E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
        }
        /// <summary>
        ///  添加返迴ID值
        ///  涂聚文 2014-12-29
        ///  Geovin Du
        /// 參考:  http://www.mikesdotnetting.com/article/54/getting-the-identity-of-the-most-recently-added-record
        /// http://stackoverflow.com/questions/186544/identity-after-insert-statement-always-returns-0
        /// </summary>
        /// <param name="SQLString"></param>
        /// <param name="identity"></param>
        /// <param name="cmdParms"></param>
        /// <returns></returns>
        public static int ExecuteSql(string SQLString, out int identity, params OleDbParameter[] cmdParms)
        {

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.CommandText = "Select @@Identity";
                        identity = (int)cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.OleDb.OleDbException E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
        }
时间: 2025-01-30 12:33:39

csharp: using OleDb Getting the identity of the most recently added record的相关文章

Modern Web Identity: Why Your Web Applications Should Be Offering OpenID, OAuth, And Probably Facebook Connect

原文地址:http://hinchcliffe.org/archive/2009/04/15/16745.aspx Are you creating a new Web site and developing a user registration system that requires new visitors to sign-up and create a user ID and password? Stop now and read this. There are now more ef

DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS &amp; PUBLISHING THEM TO THE MICROSOFT STORE

原文: DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS & PUBLISHING THEM TO THE MICROSOFT STORE UWP Apps may distributed in 2 different ways: In the Windows App Store. (This should be used for deployment of any production app)

csharp:SQLite and Access using C# code read data

SQLite sql script: CREATE TABLE BookKindList ( BookKindID INTEGER PRIMARY KEY AUTOINCREMENT, BookKindName varchar(500) not null, BookKindCode varchar(100) null, BookKindParent int null ) --添加 insert into BookKindList(BookKindName,BookKindCode,BookKin

初级:ASP.NET使用OLEDB适配器访问ACCESS数据库出现各类连接异常

access|asp.net|访问|数据|数据库 同样是一个常见问题,尤其在WIN2003下经常有人问起.出现连接异常通常是由磁盘权限引起,系统默认ASP.NET帐户对于NTSF分区只具有最小特权,因此可能引起访问异常.解决办法是于系统中修正相应权限. 另:连接字符串中指定的独占访问方式也可能引起连接异常.此情况只需修正连接字符串即可.如:conx.ConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admi

sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code

MySQL存储过程: #插入一条返回值 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $$ CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindOut` (IN param1Name NVarChar(1000),IN param1Parent Int,OUT ID INT) BEGIN IF NOT EXISTS (SELECT * FROM B

【IOS】解决XCODE项目出现MY MAC 64-BIT/设置允许用户外部资源同步到APP/解决CODESIGN ERROR: CERTIFICATE IDENTITY ‘IPHONE DISTRIBUTION***APPEARS MORE THAN ONCE IN THE KEYCHAIN .

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/621.html 本章节Himi为大家总结几点经常用到的功能或者经常出现的问题以及解决方法: 本章节主要介绍3个知识点: 1. 解决Xcode中选择设备运行项目时,设备一栏现实"My mac 64-bit"问题: 2.设置允许用户利用itunes同步一些资源到我们的App中: 3.解决CodeSign

ASP通过@@identity取得最新添加记录的ID值

set rs=server.createobject("adodb.recordset")sql="select * from table"rs.open sql,conn,1,3rs.addnewrs("abc1")=abc1rs("abc2")=abc2rs.updaters.closers.open "select @@identity",conn,1,1Response.Write rs(0)rs.

MS SQLServer OLEDB分布式事务无法启动的一般解决方案

server|sqlserver|分布式|解决 本文属spanzhang原创,其blog地址为:http://blog.csdn.net/spanzhang.引用或转贴请注明出处,谢谢!! 服务器: 消息 7391,级别 16,状态 1,行 6该操作未能执行,因为 OLE DB 提供程序 'SQLOLEDB' 无法启动分布式事务.[OLE/DB provider returned message: 不能在指定的事务处理器中获得新事务.]------------------------------

自动设置Identity属性的代码(PowerDesigner脚本)

脚本 '*****************************************************************************'文件:SetIdentity.vbs'版本:1.0'版权:floodzhu (floodzhu@hotmail.com),2004.12.31'功能:遍历物理模型中的所有表,把是主键但不是外键的字段设置为Identity,适用于'      物理模型为MS Sql Server的类型.'用法:打开物理模型,运行本脚本(Ctrl+Shi