<bdo id='TR6Qq'></bdo><ul id='TR6Qq'></ul>
    <i id='TR6Qq'><tr id='TR6Qq'><dt id='TR6Qq'><q id='TR6Qq'><span id='TR6Qq'><b id='TR6Qq'><form id='TR6Qq'><ins id='TR6Qq'></ins><ul id='TR6Qq'></ul><sub id='TR6Qq'></sub></form><legend id='TR6Qq'></legend><bdo id='TR6Qq'><pre id='TR6Qq'><center id='TR6Qq'></center></pre></bdo></b><th id='TR6Qq'></th></span></q></dt></tr></i><div id='TR6Qq'><tfoot id='TR6Qq'></tfoot><dl id='TR6Qq'><fieldset id='TR6Qq'></fieldset></dl></div>

      <tfoot id='TR6Qq'></tfoot>

      <small id='TR6Qq'></small><noframes id='TR6Qq'>

      1. <legend id='TR6Qq'><style id='TR6Qq'><dir id='TR6Qq'><q id='TR6Qq'></q></dir></style></legend>

      2. SCOPE_IDENTITY 似乎不适用于参数化查询

        时间:2023-06-06

            • <bdo id='0FtKX'></bdo><ul id='0FtKX'></ul>
              <legend id='0FtKX'><style id='0FtKX'><dir id='0FtKX'><q id='0FtKX'></q></dir></style></legend><tfoot id='0FtKX'></tfoot>

                  <tbody id='0FtKX'></tbody>

                1. <i id='0FtKX'><tr id='0FtKX'><dt id='0FtKX'><q id='0FtKX'><span id='0FtKX'><b id='0FtKX'><form id='0FtKX'><ins id='0FtKX'></ins><ul id='0FtKX'></ul><sub id='0FtKX'></sub></form><legend id='0FtKX'></legend><bdo id='0FtKX'><pre id='0FtKX'><center id='0FtKX'></center></pre></bdo></b><th id='0FtKX'></th></span></q></dt></tr></i><div id='0FtKX'><tfoot id='0FtKX'></tfoot><dl id='0FtKX'><fieldset id='0FtKX'></fieldset></dl></div>

                  <small id='0FtKX'></small><noframes id='0FtKX'>

                  本文介绍了SCOPE_IDENTITY 似乎不适用于参数化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个带有标识列的表,我想在插入后获取该列的值.以下不使用参数的代码运行良好:

                  I have a table with an identity column whose value I would like to get after an INSERT. The following code, which does not use parameters, is working perfectly:

                  string query = "INSERT INTO aTable ([aColumn]) VALUES (42)";
                  SqlCommand command = new SqlCommand(query, connection);
                  command.ExecuteNonQuery();
                  
                  query = "SELECT CAST(SCOPE_IDENTITY() AS bigint)";
                  command = new SqlCommand(query, connection);
                  object identity = command.ExecuteScalar();
                  

                  如果我将上述代码的 INSERT 部分更改为使用参数化查询,ExecuteScalar() 会突然返回一个 System.DBNull 值.这是参数化查询代码的样子:

                  If I change the INSERT part of the above code to use a parameterized query, ExecuteScalar() suddenly returns a System.DBNull value. This is how the parameterized query code looks like:

                  string query = "INSERT INTO aTable ([aColumn]) VALUES (@aColumn)";
                  SqlCommand command = new SqlCommand(query, connection);
                  command.Parameters.AddWithValue("@aColumn", 42);
                  command.ExecuteNonQuery();
                  

                  我尝试更改 SCOPE_IDENTITY 代码,以便它使用输出参数并调用 ExecuteNonQuery(),但我仍然在 out 参数中得到空值.我还尝试在两个不同版本的 SQL Server(2012 和 2008,都为 Express 版)上运行代码,结果相同.

                  I have tried to change the SCOPE_IDENTITY code so that it uses an output parameter and invokes ExecuteNonQuery(), but I still get a null value in the out parameter. I have also tried running the code against two different versions of SQL Server (2012 and 2008, both Express Edition), again with the same result.

                  知道我在这里做错了什么吗?

                  Any ideas what I am doing wrong here?

                  推荐答案

                  尝试将 INSERT 和 SELECT 合并为一个语句

                  Try combining your INSERT and SELECT into one statement

                  string query = "INSERT INTO aTable ([aColumn]) VALUES (@aColumn);SELECT CAST(SCOPE_IDENTITY() AS bigint)"; 
                  SqlCommand command = new SqlCommand(query, connection); 
                  command.Parameters.AddWithValue("@aColumn", 42); 
                  object identity = command.ExecuteScalar(); 
                  

                  这篇关于SCOPE_IDENTITY 似乎不适用于参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 ADO 连接到 Oracle 下一篇:Oracle 将行转为列

                  相关文章

                  最新文章

                  • <bdo id='Tz6PN'></bdo><ul id='Tz6PN'></ul>

                  <small id='Tz6PN'></small><noframes id='Tz6PN'>

                  <tfoot id='Tz6PN'></tfoot>
                  1. <i id='Tz6PN'><tr id='Tz6PN'><dt id='Tz6PN'><q id='Tz6PN'><span id='Tz6PN'><b id='Tz6PN'><form id='Tz6PN'><ins id='Tz6PN'></ins><ul id='Tz6PN'></ul><sub id='Tz6PN'></sub></form><legend id='Tz6PN'></legend><bdo id='Tz6PN'><pre id='Tz6PN'><center id='Tz6PN'></center></pre></bdo></b><th id='Tz6PN'></th></span></q></dt></tr></i><div id='Tz6PN'><tfoot id='Tz6PN'></tfoot><dl id='Tz6PN'><fieldset id='Tz6PN'></fieldset></dl></div>

                      <legend id='Tz6PN'><style id='Tz6PN'><dir id='Tz6PN'><q id='Tz6PN'></q></dir></style></legend>