Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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# 在Switch语句中使用update命令_C#_Asp.net_Switch Statement - Fatal编程技术网

C# 在Switch语句中使用update命令

C# 在Switch语句中使用update命令,c#,asp.net,switch-statement,C#,Asp.net,Switch Statement,我试图在switch case语句中使用并更新命令,但当我运行它时,它根本没有更新,不确定我在这里做错了什么。这是我的密码: protected void update() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString); SqlCommand Mycmd = new SqlCommand();

我试图在switch case语句中使用并更新命令,但当我运行它时,它根本没有更新,不确定我在这里做错了什么。这是我的密码:

protected void update()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
    SqlCommand Mycmd = new SqlCommand();       
    SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Name, Stage from Mytable  WHERE ID=  @ID ", con);
    DataTable dt = new DataTable();
    da.SelectCommand.Parameters.AddWithValue("@ID", (ID));
    da.Fill(dt); 

    foreach (DataRow row in dt.Rows)
    {
        switch (Convert.ToString(row["Stage"]))
        {
            case "1":                    
                string myStage= txtStageLevel.Text;
                Mycmd.CommandText = "UPDATE Mytable SET Stage=@myStage";
                break;
        }

缺少ExecuteOnQuery调用,参数@myStage的设置和连接未分配给该命令。在所有这些之后,您可能会将代码更改为

case "1":
    using(SqlCommand myCmd = new SqlCommand("UPDATE Mytable SET Stage=@myStage", con)
    {
       myCmd.Parameters.AddWithValue("@myStage", txtStageLevel.Text);
       myCmd.ExecuteNonQuery();
    }
    break;
并删除方法开头的MyCmd声明


最后,在进入开关盒之前,在代码中明确地打开连接…

那么调用
Mycmd.ExecuteNonQuery()
的代码在哪里?也缺少实际添加
@myStage
作为参数的部分;-)我投了否决票,因为一开始你在占位符中加入了一个应该是评论的答案。好吧,我马上注意到缺少ExecuteOnQuery,这几乎不是一个评论。然而,出现了其他问题,现在(在五分钟的时间里),我已经全部解释了这些问题