久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <small id='bWxTu'></small><noframes id='bWxTu'>

      <legend id='bWxTu'><style id='bWxTu'><dir id='bWxTu'><q id='bWxTu'></q></dir></style></legend>
      <i id='bWxTu'><tr id='bWxTu'><dt id='bWxTu'><q id='bWxTu'><span id='bWxTu'><b id='bWxTu'><form id='bWxTu'><ins id='bWxTu'></ins><ul id='bWxTu'></ul><sub id='bWxTu'></sub></form><legend id='bWxTu'></legend><bdo id='bWxTu'><pre id='bWxTu'><center id='bWxTu'></center></pre></bdo></b><th id='bWxTu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='bWxTu'><tfoot id='bWxTu'></tfoot><dl id='bWxTu'><fieldset id='bWxTu'></fieldset></dl></div>

        <bdo id='bWxTu'></bdo><ul id='bWxTu'></ul>
      <tfoot id='bWxTu'></tfoot>

      1. c# 如果記錄存在則更新,否則插入新記錄

        c# update if record exists else insert new record(c# 如果記錄存在則更新,否則插入新記錄)
            • <small id='AwTuu'></small><noframes id='AwTuu'>

            • <i id='AwTuu'><tr id='AwTuu'><dt id='AwTuu'><q id='AwTuu'><span id='AwTuu'><b id='AwTuu'><form id='AwTuu'><ins id='AwTuu'></ins><ul id='AwTuu'></ul><sub id='AwTuu'></sub></form><legend id='AwTuu'></legend><bdo id='AwTuu'><pre id='AwTuu'><center id='AwTuu'></center></pre></bdo></b><th id='AwTuu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='AwTuu'><tfoot id='AwTuu'></tfoot><dl id='AwTuu'><fieldset id='AwTuu'></fieldset></dl></div>
            • <tfoot id='AwTuu'></tfoot>

              1. <legend id='AwTuu'><style id='AwTuu'><dir id='AwTuu'><q id='AwTuu'></q></dir></style></legend>
                  <bdo id='AwTuu'></bdo><ul id='AwTuu'></ul>

                    <tbody id='AwTuu'></tbody>
                  本文介紹了c# 如果記錄存在則更新,否則插入新記錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  I have code that inserts data into a table when a user enters certain values into three boxes on the page.

                  The boxes are order number, total weight and tracking reference.

                  I now need to add further functionality to this code and check first to see if the order number exists, if it does i need to update the columns, if it doesn't I need to insert a new row and add data to that.

                  I was thinking simply, something like IF results = 0, Insert NEW, ELSE update

                  How can I modify my code to do this?

                  protected void Page_Load(object sender, EventArgs e)
                  {
                      errorLabel.Visible = false;
                      successLabel.Visible = false;
                      errorPanel.Visible = false;
                  }
                  
                  protected void submitBtn_Click(object sender, EventArgs e)
                  {
                      if (Page.IsValid)
                      {
                          int _orderID = Convert.ToInt32(orderID.Text);
                          string _trackingID = trackingNumber.Text;
                          DateTime _date = DateTime.UtcNow;
                          int _weightID = Convert.ToInt32(weightID.Text);
                  
                          SqlConnection myConnection = new SqlConnection("Data Source=localhost\Sqlexpress;Initial Catalog=databasename;User ID=username;Password=password");
                          SqlCommand myCommand = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
                  
                          try
                          {
                              myConnection.Open();
                              myCommand.Parameters.AddWithValue("@order", _orderID);
                              myCommand.Parameters.AddWithValue("@tracking", _trackingID);
                              myCommand.Parameters.AddWithValue("@date", _date);
                              myCommand.Parameters.AddWithValue("@weight", _weightID);
                              int rowsUpdated = myCommand.ExecuteNonQuery();
                              myConnection.Close();
                              if (rowsUpdated > 0)
                              {
                                  alertdiv.Attributes.Add("class", "alert alert-success form-signin");
                                  successLabel.Text = "Thank you, tracking details have been updated";
                                  successLabel.Visible = true;
                                  errorPanel.Visible = true;
                  
                              }
                              else
                              {
                  
                                  alertdiv.Attributes.Add("class", "alert alert-error form-signin");
                                  errorLabel.Text = "Oh dear, the order number is not recognised, please check and try again";
                                  errorLabel.Visible = true;
                                  errorPanel.Visible = true;
                              }
                  
                              orderID.Text = "";
                              trackingNumber.Text = "";
                              weightID.Text = "";
                          }
                          catch (Exception f)
                          {
                              errorLabel.Text = "This order number does not exist, please check";
                              errorLabel.Visible = true;
                              errorPanel.Visible = true;
                              return;
                  
                          }
                      }
                  }
                  
                  protected void Signout_Click(object sender, EventArgs e)
                  {
                      FormsAuthentication.SignOut();
                      Response.Redirect("Login.aspx");
                  }
                  

                  解決方案

                  You can add some SELECT query before your INSERT statement. So if the SELECT query returns more than one row, it means that you already have that record in the DB, and need to update. So, in general it will be like

                  SqlCommand cmdCount = new SqlCommand("SELECT count(*) from Shipment WHERE OrderId = @order", myConnection);
                  cmdCount.Parameters.AddWithValue("@order", _orderID);
                  int count = (int)cmdCount.ExecuteScalar();
                  
                  if (count > 0)
                  {
                       // UPDATE STATEMENT
                       SqlCommand updCommand = new SqlCommand("UPDATE Shipment SET TrackingNumber = @tracking, ShippedDateUtc = @date, TotalWeight = @weight", myConnection);
                       updCommand.Parameters.AddWithValue("@order", _orderID);
                       updCommand.Parameters.AddWithValue("@tracking", _trackingID);
                       updCommand.Parameters.AddWithValue("@date", _date);
                       updCommand.Parameters.AddWithValue("@weight", _weightID);
                       int rowsUpdated = myCommand.ExecuteNonQuery();
                  }
                  else
                  {
                       // INSERT STATEMENT
                       SqlCommand insCommand = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
                       insCommand.Parameters.AddWithValue("@order", _orderID);
                       insCommand.Parameters.AddWithValue("@tracking", _trackingID);
                       insCommand.Parameters.AddWithValue("@date", _date);
                       insCommand.Parameters.AddWithValue("@weight", _weightID);
                       int rowsUpdated = myCommand.ExecuteNonQuery();
                  }
                  

                  Edit: Or much shorter:

                  SqlCommand command;
                  
                  if (count > 0)
                  {
                       command = new SqlCommand("UPDATE Shipment SET TrackingNumber = @tracking, ShippedDateUtc = @date, TotalWeight = @weight WHERE OrderId = @order", myConnection);
                  }
                  else
                  {
                       command = new SqlCommand("INSERT into Shipment (TrackingNumber, OrderId, ShippedDateUtc, CreatedOnUtc, TotalWeight) VALUES (@tracking, @order, @date, @date, @weight)", myConnection);
                  }
                  
                  command.Parameters.AddWithValue("@order", _orderID);
                  command.Parameters.AddWithValue("@tracking", _trackingID);
                  command.Parameters.AddWithValue("@date", _date);
                  command.Parameters.AddWithValue("@weight", _weightID);
                  int rowsUpdated = command.ExecuteNonQuery();
                  

                  這篇關(guān)于c# 如果記錄存在則更新,否則插入新記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數(shù)調(diào)用按鈕 OnClick)
                    <tbody id='LvSgs'></tbody>

                    <small id='LvSgs'></small><noframes id='LvSgs'>

                    <legend id='LvSgs'><style id='LvSgs'><dir id='LvSgs'><q id='LvSgs'></q></dir></style></legend>

                      <bdo id='LvSgs'></bdo><ul id='LvSgs'></ul>
                        <i id='LvSgs'><tr id='LvSgs'><dt id='LvSgs'><q id='LvSgs'><span id='LvSgs'><b id='LvSgs'><form id='LvSgs'><ins id='LvSgs'></ins><ul id='LvSgs'></ul><sub id='LvSgs'></sub></form><legend id='LvSgs'></legend><bdo id='LvSgs'><pre id='LvSgs'><center id='LvSgs'></center></pre></bdo></b><th id='LvSgs'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='LvSgs'><tfoot id='LvSgs'></tfoot><dl id='LvSgs'><fieldset id='LvSgs'></fieldset></dl></div>
                          • <tfoot id='LvSgs'></tfoot>
                            主站蜘蛛池模板: 羞羞视频免费观 | 黄色网络在线观看 | 国产999精品久久久久久 | 亚洲人人舔人人 | 日本福利在线 | 伊人春色成人 | 久久国产精品偷 | 蜜桃综合在线 | 日韩视频一区在线观看 | 欧美色999| 老熟女毛片 | 麻豆精品国产免费 | 超碰av在线 | 亚洲欧美国产精品一区二区 | 亚州综合在线 | 欧美日韩国产不卡 | 欧美视频一区二区三区 | 成年人视频在线免费观看 | 日本人爽p大片免费看 | 中文字幕欧美日韩 | 久久精品中文 | 亚洲欧美日韩成人在线 | 成人影院午夜 | 久久久久久久久国产 | 日日干夜夜操天天操 | 欧美日韩国产一区二区三区不卡 | 国产精品久久国产精品 | 亚洲精品欧美一区二区三区 | 国产国拍亚洲精品av | 91欧美激情一区二区三区成人 | 国产精品成人一区二区三区夜夜夜 | 日韩一区二区三区视频 | 天天草夜夜骑 | 国产精品国产三级国产aⅴ入口 | 国产亚洲欧美在线视频 | 亚洲日本欧美日韩高观看 | 欧美日韩不卡在线 | 日韩一区中文字幕 | 天天干视频 | 在线播放国产一区二区三区 | 国产乱码精品一区二区三区五月婷 |