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);
                    }
                }
            }
        }

  

时间: 2024-07-31 14:34:41

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

OpenStack Identity API v3 extensions (CURRENT)

Table Of Contents Identity API v3 extensions (CURRENT) OS-ENDPOINT-POLICY API Associate policy and endpoint Verify a policy and endpoint association Delete a policy and endpoint association Associate policy and service-type endpoint Verify a policy a

ASP.NET生成WORD文档,服务器部署注意事项

网上转的,留查备用,我服务器装的office2007所以修改的是Microsoft Office word97 - 2003 文档这一个. ASP.NET生成WORD文档服务器部署注意事项 1.Asp.net 2.0在配置Microsoft Excel.Microsoft Word应用程序权限时 error: 80070005 和8000401a 的解决总   2007-11-01 11:30  检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-00000

Rewrite MSIL Code on the Fly with the .NET Framework Profiling API

.NET Internals Rewrite MSIL Code on the Fly with the .NET Framework Profiling API Aleksandr Mikunov This article assumes you're familiar with the CLR and C# Level of Difficulty 1 2 3 Code download available at: NETProfilingAPI.exe (2,901KB) SUMMARY I

【TensorFlow_Fold】深度探究 Blocks for Composition

0xFF 未完工说明 Composition某种意义上可以说是基本.核心的一部分,打算一点点慢慢写,无奈Src有点多,我想继续边看边思考,敬请原谅~o(∩_∩)o~ [当前进度 89%] 0x00 前言 想写点东西试试,结果接下来就老老实实躺在了Pipeline上: 决定学跑之前先学爬,老老实实啃一下源码和官方文档,虽然官方还在一点点更新,不少地方还是空白的,不过先动起来多敲点试试看,老等着别人喂饭多不好呀(对呀我也觉得连前言都要重复我真的好懒呀23333): TFF的基本单位之一是Block,

Regex Failure - Bug Fixing #2

http://www.codewars.com/kata/55c423ecf847fbcba100002b/train/csharp Oh no, Timmy's received some hate mail recently but he knows better. Help timmy fix his regex filter so he can be awesome again! using System; using NUnit.Framework; System.Text.Regul

[转] 英语论文写作技巧-3

英语论文写作技巧-3(字的使用中特别注意事项) 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 http://muchong.com/html/200906/1393920.html 用字要准确 每个科学术语都有其特定的含义, 使用要准确. 推理用语的使用 从实验观察和数据到结论的推理过程中,在事实和理论的关系上可能有从"同-一致","表示","证明"等不同的强弱关系,在选择用词上要合理. 英语中经常使用的词是is compat

2019省赛训练组队赛4.9周二 2017浙江省赛

A - Cooking Competition "Miss Kobayashi's Dragon Maid" is a Japanese manga series written and illustrated by Coolkyoushinja. An anime television series produced by Kyoto Animation aired in Japan between January and April 2017. In episode 8, two

Elasticsearchdump 数据导入/导出

一.安装过程 Elasticsearchdump 仓库地址,详细使用情况 当前工具主要是用来对ES中的数据进行数据导入/导出,以及对数据迁移相关,使用elasticdump工具需要使用到npm,所以需要安装相关的依赖 目前使用到的ES版本是7.x 安装NODE NODE和NPM安装链接详细文档 安装命令如下: $ wget https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xz $ tar -xf node-v10.15.0

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