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

    1. <tfoot id='oEhZD'></tfoot>

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

          <bdo id='oEhZD'></bdo><ul id='oEhZD'></ul>
      1. 使用 c# 从 SQL Server 插入命令返回值

        时间:2023-06-04
        • <tfoot id='OBM6o'></tfoot>

            <tbody id='OBM6o'></tbody>

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

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

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

                  <bdo id='OBM6o'></bdo><ul id='OBM6o'></ul>
                  本文介绍了使用 c# 从 SQL Server 插入命令返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  在 Visual Studio 中使用 C#,我将一行插入到这样的表中:

                  Using C# in Visual Studio, I'm inserting a row into a table like this:

                  INSERT INTO foo (column_name)
                  VALUES ('bar')
                  

                  我想做这样的事情,但我不知道正确的语法:

                  I want to do something like this, but I don't know the correct syntax:

                  INSERT INTO foo (column_name)
                  VALUES ('bar')
                  RETURNING foo_id
                  

                  这将从新插入的行返回 foo_id 列.

                  This would return the foo_id column from the newly inserted row.

                  此外,即使我找到了正确的语法,我还有另一个问题:我可以使用 SqlDataReaderSqlDataAdapter.据我所知,前者用于读取数据,后者用于操作数据.在插入带有 return 语句的行时,我既在操作又在读取数据,所以我不确定该使用什么.也许我应该为此使用完全不同的东西?

                  Furthermore, even if I find the correct syntax for this, I have another problem: I have SqlDataReader and SqlDataAdapter at my disposal. As far as I know, the former is for reading data, the second is for manipulating data. When inserting a row with a return statement, I am both manipulating and reading data, so I'm not sure what to use. Maybe there's something entirely different I should use for this?

                  推荐答案

                  SCOPE_IDENTITY 返回插入到同一范围内的标识列中的最后一个标识值.范围是一个模块:存储过程、触发器、函数或批处理.因此,如果两条语句在同一个存储过程、函数或批处理中,则它们属于同一范围.

                  SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

                  您可以使用 SqlCommand.ExecuteScalar 执行插入命令并在一个查询中检索新 ID.

                  You can use SqlCommand.ExecuteScalar to execute the insert command and retrieve the new ID in one query.

                  using (var con = new SqlConnection(ConnectionString)) {
                      int newID;
                      var cmd = "INSERT INTO foo (column_name)VALUES (@Value);SELECT CAST(scope_identity() AS int)";
                      using (var insertCommand = new SqlCommand(cmd, con)) {
                          insertCommand.Parameters.AddWithValue("@Value", "bar");
                          con.Open();
                          newID = (int)insertCommand.ExecuteScalar();
                      }
                  }
                  

                  这篇关于使用 c# 从 SQL Server 插入命令返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SQL 命令 INSERT 正在工作,但数据未出现在表中 下一篇:在 INSERT 上返回 ID?

                  相关文章

                  最新文章

                    <bdo id='8mA3G'></bdo><ul id='8mA3G'></ul>

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

                    <small id='8mA3G'></small><noframes id='8mA3G'>

                    <tfoot id='8mA3G'></tfoot>
                  1. <legend id='8mA3G'><style id='8mA3G'><dir id='8mA3G'><q id='8mA3G'></q></dir></style></legend>