2017-04-18 83 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。花时间做出回应。这是非常赞赏。 –