Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SCOPE_INDENTITY()不工作是否返回null?_C#_.net_Sql_C# 4.0_Ado.net - Fatal编程技术网

C# SCOPE_INDENTITY()不工作是否返回null?

C# SCOPE_INDENTITY()不工作是否返回null?,c#,.net,sql,c#-4.0,ado.net,C#,.net,Sql,C# 4.0,Ado.net,在阅读了这个站点之后,我认为下面的代码将返回我添加到数据库中的项的ID,但是我尝试填充的int值没有显示在范围中。以下是我的insert语句的代码: foreach (ExOb exc in exobject) { Type type = exc.GetType(); //Add a weight exercise if (type == typeof(Weights))

在阅读了这个站点之后,我认为下面的代码将返回我添加到数据库中的项的ID,但是我尝试填充的int值没有显示在范围中。以下是我的insert语句的代码:

  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 

我在这里做错了什么?

我假设该表有一个标识列

尝试这样做:

string InsertWeight = ("INSERT INTO WeightExercise 
(ExcerciseName, Duration, Sets, DiaryId) VALUES (@name, @time, @sets, @DiaryId); SELECT SCOPE_IDENTITY()");
然后使用int-ID=int-com.ExecuteScalar代替com.ExecuteNonQuery


对于实际问题,请尝试在insert语句之后和集合之前加上分号。原因是因为你在同一份声明中。调用SCOPE_IDENTITY时,还没有插入。您需要终止该声明;然后调用SCOPE_IDENTITY

我假设该表有一个标识列

尝试这样做:

string InsertWeight = ("INSERT INTO WeightExercise 
(ExcerciseName, Duration, Sets, DiaryId) VALUES (@name, @time, @sets, @DiaryId); SELECT SCOPE_IDENTITY()");
然后使用int-ID=int-com.ExecuteScalar代替com.ExecuteNonQuery

对于实际问题,请尝试在insert语句之后和集合之前加上分号。原因是因为你在同一份声明中。调用SCOPE_IDENTITY时,还没有插入。您需要终止该声明;然后调用SCOPE_IDENTITY

SCOPE\u IDENTITY返回插入同一作用域中标识列的最后一个标识值。您需要使用ExecuteScalar而不是ExecuteOnQuery来检索它:

因此,请先将sql更改为:

插入到举重练习名称、持续时间、集合、日记ID中 值@name、@time、@sets、@DiaryId;选择范围和标识

然后,您可以通过以下方式检索id:

ID = (int)com.ExecuteScalar();
SCOPE_IDENTITY返回插入同一作用域中标识列的最后一个标识值。您需要使用ExecuteScalar而不是ExecuteOnQuery来检索它:

因此,请先将sql更改为:

插入到举重练习名称、持续时间、集合、日记ID中 值@name、@time、@sets、@DiaryId;选择范围和标识

然后,您可以通过以下方式检索id:

ID = (int)com.ExecuteScalar();
我已经检查了调试的时间,命令参数@ID保存了 最新条目的ID

这表示您当前使用的数据库代码运行正常

调试时Int ID不会在本地显示,如果我观看它,我会 得到

这是你问题的根源。问题是您已将ID的作用域设置为

if (type == typeof(Weights))
街区。在该块之外,标识符ID没有任何意义

在此过程的作用域顶部声明ID,您应该能够在局部变量中看到它或添加一个监视

我已经检查了调试的时间,命令参数@ID保存了 最新条目的ID

这表示您当前使用的数据库代码运行正常

调试时Int ID不会在本地显示,如果我观看它,我会 得到

这是你问题的根源。问题是您已将ID的作用域设置为

if (type == typeof(Weights))
街区。在该块之外,标识符ID没有任何意义


在此过程的作用域顶部声明ID,您应该能够在局部变量中看到它或添加一个手表。

这是范围问题吗?ID仅存在于if type==typeofWeights块中我只使用了一个select scope\u标识来完成此操作,但我看不出您的代码有什么问题。这里有标识列吗?这是范围问题吗?ID仅存在于if type==typeofWeights块中。我只使用了一个select scope_标识完成了此操作,但我看不出代码有什么问题。这里是否有标识列?OP正在更新指定为输出的参数-它本来应该很好,对于ExecuteOnQuery,OP正在更新一个指定为输出的参数-它本来应该很好,对于ExecuteOnQueryThis,尽管所有其他答案都非常有用-移动声明并没有在局部变量或观察中显示出来,但完成我的代码并使用它更新另一个表中的列确实如此。感谢并为这个毫无意义的问题道歉!!!虽然所有其他答案都非常有用,但这就是问题所在——移动声明并没有在Locals或watch中显示出来,而是完成了我的代码并使用它更新了另一个表中的列。感谢并为这个毫无意义的问题道歉!!!