2

如果记录存在,我正在尝试构建更新语句,否则插入新记录。我有一个可以使插入正常的 DAL,但现在我需要为现有案例添加更新。我有这个用于插入的工作代码:

    public static void saveArticle(DateTime mtime, int mid, string mname, double homeodds, double awayodds, double drawodds)
    {
       var culture = CultureInfo.GetCultureInfo("sv-SE");
       var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
       var dateTime = DateTime.Today;

       int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
       string mroundY = dateTime.Year.ToString();
       string mroundW = weekNumber.ToString();
       string mround = mroundY + mroundW;

       //Console.WriteLine(mround);

       SqlCommand saveArticle = new SqlCommand("Insert Into gameTB(mround, mtime, mid, mname, homeodds, awayodds, drawodds) VALUES (@mround,@mtime,@mid,@mname,@homeodds,@awayodds,@drawodds)");
       saveArticle.Parameters.Add("@mround", SqlDbType.Int).Value = mround;
       saveArticle.Parameters.Add("@mtime", SqlDbType.DateTime).Value = mtime;
       saveArticle.Parameters.Add("@mid", SqlDbType.Int).Value = mid;
       saveArticle.Parameters.Add("@mname", SqlDbType.VarChar, 255).Value = mname;
       saveArticle.Parameters.Add("@homeodds", SqlDbType.Float).Value = homeodds;
       saveArticle.Parameters.Add("@awayodds", SqlDbType.Float).Value = awayodds;
       saveArticle.Parameters.Add("@drawodds", SqlDbType.Float).Value = drawodds;

       DAL.ExecuteNonQuery(saveArticle);
    }

阅读此论坛帖子后,我尝试添加 IF 语句,但它不适用于我使用的逻辑。

如何使用已有的 else insert 命令完成更新?

4

5 回答 5

2

基于这个INSERT vs. UPDATE我设法让它工作。我关注了一篇类似的文章,该文章留下了对我来说并不明显的基本想法。我以前试过这个,但我犯了一些大错误,所以我认为我偏离了轨道。这是我的工作代码;

    SqlCommand saveArticle = new SqlCommand("if Exists(Select 1 from gameTB where mround = @mround AND mid = @mid)\r\n" +
    "UPDATE gameTB SET homeodds = @homeodds, awayodds = @awayodds, drawodds = @drawodds Where mround = @mround AND mid=@mid\r\n" +
    "else\r\n" +
    "Insert Into gameTB(mround, mtime, mid, mname, homeodds, awayodds, drawodds) VALUES (@mround,@mtime,@mid,@mname,@homeodds,@awayodds,@drawodds)");

    saveArticle.Parameters.Add("@mround", SqlDbType.Int).Value = mround;
    saveArticle.Parameters.Add("@mtime", SqlDbType.DateTime).Value = mtime;
    saveArticle.Parameters.Add("@mid", SqlDbType.Int).Value = mid;
    saveArticle.Parameters.Add("@mname", SqlDbType.VarChar, 255).Value = mname;
    saveArticle.Parameters.Add("@homeodds", SqlDbType.Float).Value = homeodds;
    saveArticle.Parameters.Add("@awayodds", SqlDbType.Float).Value = awayodds;
    saveArticle.Parameters.Add("@drawodds", SqlDbType.Float).Value = drawodds;

    DAL.ExecuteNonQuery(saveArticle);
于 2013-03-05T23:13:04.533 回答
0

这是一个 MS SQL 2012。我研究了 MERGE 命令,但不明白如何使用它。

我曾尝试这样做,但在尝试构建时收到很多错误,我猜我已经偏离轨道了,对吧?

    SqlCommand updateArticle = new SqlCommand("UPDATE gameTB SET homeodds = @homeodds, awayodds = @awayodds, drawodds = @drawodds Where mround = @mround AND mid=@mid");
    DAL.ExecuteNonQuery(updateArticle);
    if (@@rowcount = 0) Then
    SqlCommand saveArticle = new SqlCommand("Insert Into gameTB(mround, mtime, mid, mname, homeodds, awayodds, drawodds) VALUES (@mround,@mtime,@mid,@mname,@homeodds,@awayodds,@drawodds)");
    saveArticle.Parameters.Add("@mround", SqlDbType.Int).Value = mround;
    saveArticle.Parameters.Add("@mtime", SqlDbType.DateTime).Value = mtime;
    saveArticle.Parameters.Add("@mid", SqlDbType.Int).Value = mid;
    saveArticle.Parameters.Add("@mname", SqlDbType.VarChar, 255).Value = mname;
    saveArticle.Parameters.Add("@homeodds", SqlDbType.Float).Value = homeodds;
    saveArticle.Parameters.Add("@awayodds", SqlDbType.Float).Value = awayodds;
    saveArticle.Parameters.Add("@drawodds", SqlDbType.Float).Value = drawodds;
    DAL.ExecuteNonQuery(saveArticle);
    EndIf;
于 2013-03-05T22:12:13.390 回答
0

假设您使用的是 SQL Server,您可以按如下方式重写您的命令

var saveArticle = new SqlCommand(@"UPDATE gameTB SET homeodds = @homeodds, awayodds = @awayodds, drawodds = @drawodds Where mround = @mround AND mid=@mid
if (@@rowcount = 0)
    Insert Into gameTB(mround, mtime, mid, mname, homeodds, awayodds, drawodds) VALUES (@mround,@mtime,@mid,@mname,@homeodds,@awayodds,@drawodds)");

@@rowcount包含受最后一条语句影响的记录数。因此,如果update成功,它将包含1并且insert不会被执行。

于 2013-03-05T21:57:24.480 回答
0

如果你想使用 MERGE 只需像这样设置你的命令

SqlCommand saveArticle = new SqlCommand(@"
    MERGE INTO gametb AS target 
    using ( VALUES (@mround, 
          @mtime, 
          @mid, 
          @mname, 
          @homeodds, 
          @awayodds, 
          @drawodds ) ) AS source (mround, mtime, mid, mname, homeodds, awayodds, 
          drawodds) 
    ON ( source.mround = target.mround 
         AND source.mid = target.mid ) 
    WHEN matched THEN 
      UPDATE SET mtime = source.mtime, 
                 mname = source.mname, 
                 homeodds = source.homeodds, 
                 awayodds = source.awayodds, 
                 drawodds = source.drawodds 
    WHEN NOT matched THEN 
      INSERT (mround, 
              mtime, 
              mid, 
              mname, 
              homeodds, 
              awayodds, 
              drawodds ) 
      VALUES (source.mround, 
              source.mtime, 
              source.mid, 
              source.mname, 
              source.homeodds, 
              source.awayodds, 
              source.drawodds );"); 
于 2013-03-05T22:28:59.287 回答
0

我看到你mid的代码中有 。如果这是id数据库中的记录并且您没有使用任何其他记录id(自动生成的记录),那么最简单的方法是在存在具有相同 ID 的记录时执行选择查询。如果选择查询返回一些东西,你应该更新,如果没有,则插入。

于 2013-03-05T21:55:01.123 回答