C# 使用ADO.NET更新一行中的多个列
我正在做一个项目(不是公开的),我正在尝试使用ADO.NET更新我的数据库代码。我已经为insert、retrieve all、retrieve by ID和retrieve by status编写了工作代码,我似乎无法理解的是更新。我已经做了相当多的搜索,我的代码是我找到的信息的代表,并试图适应我的程序。我只是使用垃圾数据,以便在使用任何有形数据之前尝试查看更新 下面是我在一个专门用于查询的类中的查询代码C# 使用ADO.NET更新一行中的多个列,c#,mysql,ado.net,C#,Mysql,Ado.net,我正在做一个项目(不是公开的),我正在尝试使用ADO.NET更新我的数据库代码。我已经为insert、retrieve all、retrieve by ID和retrieve by status编写了工作代码,我似乎无法理解的是更新。我已经做了相当多的搜索,我的代码是我找到的信息的代表,并试图适应我的程序。我只是使用垃圾数据,以便在使用任何有形数据之前尝试查看更新 下面是我在一个专门用于查询的类中的查询代码 public Ticket UpdateTicket(int id, string st
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 TicketId=@id";
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));
我的最终目标是能够使用这一行上面的代码硬编码更新,然后将其与一些控制台提示一起使用,以允许更新信息。然而,即使无法硬编码解决方案,我也无法考虑用户输入版本 您的更新声明似乎有误。 请尝试以下方法:
UPDATE Ticket
SET Status = @Status,
HelpDeskStaffId = @HelpDeskStaffId,
ProblemDesc = @ProblemDesc,
Resolution = @Resolution,
FollowUpRequired = @FollowUpRequired,
FollowUpComplete = @FollowUpComplete,
TicketDate = @TicketDate,
ResolvedDate = @ResolvedDate,
CustomerId = @CustomerId
WHERE TicketId=@id;
更新正如Alex 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 TicketId=@id";
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;
}
你的更新声明似乎是错误的。 请尝试以下方法:
UPDATE Ticket
SET Status = @Status,
HelpDeskStaffId = @HelpDeskStaffId,
ProblemDesc = @ProblemDesc,
Resolution = @Resolution,
FollowUpRequired = @FollowUpRequired,
FollowUpComplete = @FollowUpComplete,
TicketDate = @TicketDate,
ResolvedDate = @ResolvedDate,
CustomerId = @CustomerId
WHERE TicketId=@id;
更新正如Alex 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 TicketId=@id";
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;
}
你在哪里执行命令?您似乎也在使用现有值+新值进行更新?这真的是你想做的吗?@alexK。很抱歉,忘了提到执行发生在Program(driver/Main)类中。现在我看到收集了两次值,我正在尝试输入其他ADO.NET线程中使用的方法。我的意思是
cmdInsert.ExecuteNonQuery()
?如果你不执行,那么预期的行为就是不执行。@AlexK。仍在学习中,这行代码+Zohar Peled建议的更改解决了问题。您在哪里执行该命令?您似乎也在使用现有值+新值进行更新?这真的是你想做的吗?@alexK。很抱歉,忘了提到执行发生在Program(driver/Main)类中。现在我看到收集了两次值,我正在尝试输入其他ADO.NET线程中使用的方法。我的意思是cmdInsert.ExecuteNonQuery()
?如果你不执行,那么预期的行为就是不执行。@AlexK。仍然在学习,这句话+Zohar Peled建议的改变解决了这个问题。尝试这个,没有运气。没有收到任何错误。谢谢你和AlexK。感谢你花时间回应。非常感谢。很高兴能帮助:-)尝试这个,运气不好。没有收到任何错误。谢谢你和AlexK。感谢你花时间回应。非常感谢。很高兴帮助:-)