0

(var query4) 是否有任何问题,因为它没有向我显示任何味精,但它无法将数据插入到相关表中

@{
    var userId = Request["UserId"];
    var Type = Request["type"];
    var db = Database.Open("intranet");
    if(Type == "delete")
    {

    var query = "UPDATE Personne SET Demande = 'refuser' WHERE UserId = '" + userId + "'";
    db.Execute(query);

     var query2 = "DELETE from DemandeConge where UserId = '" + userId + "'";
    db.Execute(query2);
   }
    else if(Type == "accepte")
    {


        var query = "UPDATE Personne SET Demande = 'accepte' WHERE UserId = '" + userId + "'";
        db.Execute(query);

        var query2 = "DELETE from DemandeConge where UserId = '" + userId + "'";
        db.Execute(query2);


        var query4 = "INSERT INTO CongeAccept(UserId,DateDebut,DateFin,TypeConge) SELECT UserId,DateDebutDemande,DateFinDemande,TypeConge FROM DemandeConge WHERE UserId = '" + userId + "'";
        db.Execute(query4);
    }
}

当我对此代码发表评论时,它也可以正常工作:

   /* var query = "UPDATE Personne SET Demande = 'accepte' WHERE UserId = '" + userId + "'";
    db.Execute(query);

    var query2 = "DELETE from DemandeConge where UserId = '" + userId + "'";
    db.Execute(query2);*/


    var query4 = "INSERT INTO CongeAccept(UserId,DateDebut,DateFin,TypeConge) SELECT UserId,DateDebutDemande,DateFinDemande,TypeConge FROM DemandeConge WHERE UserId = '" + userId + "'";
    db.Execute(query4);
} 
4

1 回答 1

0

您正在从 DemandeConge 中删除与您要插入到 CongeAccept 中的用户相关的所有内容,因此当尝试插入查询时,没有任何内容可以插入。更改语句的顺序并使用参数:

@{
    var userId = Request["UserId"];
    var Type = Request["type"];
    var db = Database.Open("intranet");
    if(Type == "delete")
    {

    var query = "UPDATE Personne SET Demande = 'refuser' WHERE UserId = @0";
    db.Execute(query, userId);

     var query2 = "DELETE from DemandeConge where UserId = @0";
    db.Execute(query2, userId);
   }
    else if(Type == "accepte")
    {
        var query = "UPDATE Personne SET Demande = 'accepte' WHERE UserId = @0";
        db.Execute(query, userId);

        var query4 = "INSERT INTO CongeAccept(UserId,DateDebut,DateFin,TypeConge) SELECT UserId,DateDebutDemande,DateFinDemande,TypeConge FROM DemandeConge WHERE UserId = @0";
        db.Execute(query4, userId);

        var query2 = "DELETE from DemandeConge where UserId = @0";
        db.Execute(query2, userId);
    }
}
于 2012-02-28T07:13:03.663 回答