【问题标题】:SCOPE_INDENTITY() not working is it returning null?SCOPE_INDENTITY() 不起作用是返回 null 吗?
【发布时间】:2012-11-05 21:41:10
【问题描述】:

阅读了这个网站后,我认为以下代码会返回我添加到数据库中的项目的 ID,但我尝试填充的 int 值未显示在范围内。这是我的插入语句的代码:

  foreach (ExOb exc in exobject)
            {

                Type type = exc.GetType();
                //Add a weight exercise
                if (type == typeof(Weights))
                {

                    Weights thisweight = (Weights)exc;
                    string InsertWeight = ("INSERT INTO WeightExercise (ExcerciseName, Duration, Sets, DiaryId) VALUES (@name, @time, @sets, @DiaryId) SET @ID = SCOPE_IDENTITY();");

                        com = new SqlCommand(InsertWeight, con);
                        com.Parameters.AddWithValue("@name", thisweight.ExcerciseName);
                        com.Parameters.AddWithValue("@time", thisweight.WeightDuration);
                        com.Parameters.AddWithValue("@sets", thisweight.TotalSets);
                        com.Parameters.AddWithValue("@DiaryId", results[0]);
                        com.Parameters.Add("@ID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        con.Open();
                        com.ExecuteNonQuery();
                        int ID = (int)com.Parameters["@ID"].Value;

                        con.Close();


                }
                else if (type == typeof(Cardio))
                {
                    Cardio thiscardio = (Cardio)exc;


                }

            }
        }

数据库已更新为体重锻炼的详细信息。我在调试时检查过,命令参数@ID 包含最新条目的ID。调试时,Int ID 不会显示在本地,如果我观看它,我会得到:

    ID  The name 'ID' does not exist in the current context 

我在这里做错了什么?

【问题讨论】:

  • 这是范围界定问题吗? ID 仅存在于 if (type == typeof(Weights)) 块中
  • 我只用一个 select scope_identity() 完成了这个,但我看不出你的代码有什么问题。
  • 这里有IDENTITY 栏目吗?

标签: c# .net sql c#-4.0 ado.net


【解决方案1】:

SCOPE_IDENTITY 返回插入到同一范围内的标识列中的最后一个标识值。您需要使用ExecuteScalar 而不是ExecuteNonQuery 来检索它:

所以先把你的sql改成:

"INSERT INTO WeightExercise (ExcerciseName, Duration, Sets, DiaryId) 值(@name、@time、@sets、@DiaryId); SELECT SCOPE_IDENTITY();"

那么你可以通过这种方式检索id:

ID = (int)com.ExecuteScalar();

【讨论】:

  • OP 正在更新一个指定为输出的参数 - 应该没问题,使用 executenonquery
【解决方案2】:

我在调试时检查过,命令参数@ID 包含 最新条目的 ID。

这表明您当前使用的数据库代码运行良好。

在调试时,Int ID 不会出现在本地,如果我观看它,我 得到...

这是您问题的根源。问题是您已将 ID 限定在

if (type == typeof(Weights))

块。在该块之外,标识符ID 没有任何意义。

在此过程的范围顶部声明 ID,您应该能够在 Locals 中看到它或添加 Watch。

【讨论】:

  • 这就是它,尽管所有其他答案都非常有帮助 - 移动声明并没有在 Locals 或 watch 中显示,但完成我的代码并使用它来更新另一个表中的列确实。感谢并为有点毫无意义的问题道歉!!!
【解决方案3】:

我假设该表有一个标识列。

尝试这样做:

string InsertWeight = ("INSERT INTO WeightExercise 
(ExcerciseName, Duration, Sets, DiaryId) VALUES (@name, @time, @sets, @DiaryId); SELECT SCOPE_IDENTITY()");

然后用int ID = (int) com.ExecuteScalar()代替com.ExecuteNonQuery();

对于您的实际问题,请尝试在您的插入语句之后和 SET 之前放置一个分号。原因是因为你在同一个语句中。当 SCOPE_IDENTITY() 被调用时,还没有插入。您需要终止语句(;),然后调用SCOPE_IDENTITY();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-11
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多