2017-04-18 84 views
0

我正在開發一個項目(不是公開的),我試圖使用ADO.NET更新我的數據庫代碼。我已經寫入了用於插入的工作代碼,全部檢索,按ID檢索,按狀態檢索,我似乎無法弄清的是更新。我做了很多搜索,我擁有的代碼是我找到並嘗試適應我的程序的信息的代表。我只是使用垃圾數據才能在使用任何有形數據之前嘗試查看更新。使用ADO.NET連續更新多列

以下是我在專用於查詢的類中的查詢代碼。

public Ticket UpdateTicket(int id, string status, int customerId, int helpDeskStaffId, string problemDesc, string resolution, string followUpRequired, string followUpComplete, DateTime ticketDate, DateTime resolvedDate) 
    { 
     Ticket ticket = new Ticket(id, status, customerId, helpDeskStaffId, problemDesc, resolution, followUpRequired, followUpComplete, ticketDate, resolvedDate); 
     SqlCommand cmdInsert; 
     SqlConnection conn = null; 
     try 
     { 
      string connectString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; 
      conn = new SqlConnection(connectString); 
      "UPDATE Ticket SET Status = @Status, HelpDeskStaffId = @HelpDeskStaffId, ProblemDesc = @ProblemDesc, Resolution = @Resolution, FollowUpRequired = @FollowUpRequired, FollowUpComplete = @FollowUpComplete, TicketDate = @TicketDate, ResolvedDate = @ResolvedDate, CustomerId = @CustomerId WHERE [email protected]"; 
      conn.Open(); 
      cmdInsert = new SqlCommand(sql2, conn); 
      cmdInsert.Parameters.AddWithValue("@id", id); 
      cmdInsert.Parameters.AddWithValue("@Status", status); 
      cmdInsert.Parameters.AddWithValue("@HelpDeskStaffId", helpDeskStaffId); 
      cmdInsert.Parameters.AddWithValue("@ProblemDesc", problemDesc); 
      cmdInsert.Parameters.AddWithValue("@Resolution", resolution); 
      cmdInsert.Parameters.AddWithValue("@FollowUpRequired", followUpRequired); 
      cmdInsert.Parameters.AddWithValue("@FollowUpComplete", followUpComplete); 
      cmdInsert.Parameters.AddWithValue("@TicketDate", ticketDate); 
      cmdInsert.Parameters.AddWithValue("@ResolvedDate", resolvedDate); 
      cmdInsert.Parameters.AddWithValue("@CustomerId", customerId); 

     } 
     catch (SqlException ex) 
     { 
      Console.Error.WriteLine(ex.Message); 
     } 
     finally 
     { 
      if (conn != null) 
       conn.Close(); 
     } 
     return ticket; 
    } 

在與輸入的ID相關的行內沒有數據正在更新。

TicketUtil ticketUtil = new TicketUtil(); 
      Ticket ticket = ticketUtil.UpdateTicket(6, "Open", 1, 3, "Broken Pencils", "Buy New One", "No", "No", DateTime.Today, new DateTime(1753, 1, 1)); 

我的最終目標是要能夠使用硬編碼上面這行代碼的更新,然後使用一些控制檯會提示允許的信息更新。但是,即使無法對解決方案進行硬編碼,我甚至不能考慮用戶輸入版本。

+2

你在哪裏* *執行命令?你似乎也在用現有的價值+新的價值進行更新?那真的是你想要做的嗎? –

+0

@alexK。抱歉,忘記提及程序(驅動程序/主要)課程正在執行。我現在看到這些值收集了兩次,我試圖輸入在其他ADO.NET線程中使用的方法。 –

+0

我的意思是'cmdInsert.ExecuteNonQuery()'在哪裏?如果你不執行預期的行爲,畢竟不是執行。 –

回答

0

您的更新聲明似乎是錯誤的。 試試這個:

UPDATE Ticket 
SET Status = @Status, 
HelpDeskStaffId = @HelpDeskStaffId, 
ProblemDesc = @ProblemDesc, 
Resolution = @Resolution, 
FollowUpRequired = @FollowUpRequired, 
FollowUpComplete = @FollowUpComplete, 
TicketDate = @TicketDate, 
ResolvedDate = @ResolvedDate, 
CustomerId = @CustomerId 
WHERE [email protected]; 

更新
亞歷克斯K.在他的評論中寫道,你不執行命令。

這是我想你的代碼應該是這樣的:

public Ticket UpdateTicket(int id, string status, int customerId, int helpDeskStaffId, string problemDesc, string resolution, string followUpRequired, string followUpComplete, DateTime ticketDate, DateTime resolvedDate) 
{ 
    var ticket = new Ticket(id, status, customerId, helpDeskStaffId, problemDesc, resolution, followUpRequired, followUpComplete, ticketDate, resolvedDate); 

    var connectString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;    
    var sql2 = "UPDATE Ticket SET Status = @Status, HelpDeskStaffId = @HelpDeskStaffId, ProblemDesc = @ProblemDesc, Resolution = @Resolution, FollowUpRequired = @FollowUpRequired, FollowUpComplete = @FollowUpComplete, TicketDate = @TicketDate, ResolvedDate = @ResolvedDate, CustomerId = @CustomerId WHERE [email protected]"; 

    using(var conn = new SqlConnection(connectString)) 
    { 
     using(var cmd = new SqlCommand(sql2, conn)) 
     { 
     cmd.Parameters.AddWithValue("@id", id); 
     cmd.Parameters.AddWithValue("@Status", status); 
     cmd.Parameters.AddWithValue("@HelpDeskStaffId", helpDeskStaffId); 
     cmd.Parameters.AddWithValue("@ProblemDesc", problemDesc); 
     cmd.Parameters.AddWithValue("@Resolution", resolution); 
     cmd.Parameters.AddWithValue("@FollowUpRequired", followUpRequired); 
     cmd.Parameters.AddWithValue("@FollowUpComplete", followUpComplete); 
     cmd.Parameters.AddWithValue("@TicketDate", ticketDate); 
     cmd.Parameters.AddWithValue("@ResolvedDate", resolvedDate); 
     cmd.Parameters.AddWithValue("@CustomerId", customerId); 
     try 
     { 
      conn.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
     catch (SqlException ex) 
     { 
      Console.Error.WriteLine(ex.Message); 
     } 
     } 
    } 
    return ticket; 
} 
+0

更新嘗試這個,沒有運氣。沒有收到任何錯誤。 –

+0

我編輯了我的答案。 –

+0

謝謝你,謝謝AlexK。花時間做出迴應。這是非常讚賞。 –