在Access中工作时,UPDATE查询不会在C#中更新

时间:2017-12-04 12:53:00

标签: c# ms-access

当列中的值高于0时,我尝试更新列,否则它会插入新行。

UPDATE查询首先在Access中进行测试,然后才能运行,在C#中进行了尝试,并且确实没有问题。

不知道在哪里寻找。任何帮助/提示?

Reservation selection = (Reservation)lstbReservations.SelectedItem;
connection.Open();

command.CommandText = @"SELECT Service_Overzicht.OverzichtID, Service.ServiceID, Service.Beschrijving,
                        IIf(Service_Overzicht.Aantal Is Null,0,Service_Overzicht.Aantal) AS Aantal, Service.Prijs, Service.Categorie
                        FROM Service LEFT JOIN (SELECT * FROM Service_Overzicht WHERE OverzichtID = [@waarde])
                        AS OSF ON Service.ServiceID = OSF.ServiceID;";

command.Connection = connection;

command.Parameters.Add(new OleDbParameter("@waarde", OleDbType.Integer));
command.Parameters["@waarde"].Value = selection.Reserveringsnummer;

reader = command.ExecuteReader();
while(reader.Read())
{
    if(reader.GetString(2) == "Douche Muntjes")
    {
        if(reader.GetInt32(3) == 0)
        {
            command1.CommandText = @"INSERT INTO Service_Overzicht (OverzichtID, ServiceID, Aantal) VALUES (@resID, @sID, @aantal)";
        }
        else
        {
            command1.CommandText = @"UPDATE Service_Overzicht
                                    SET Aantal = @aantal
                                    WHERE OverzichtID = @resID AND ServiceID = @sID";
        }

        command1.Connection = connection;

        command1.Parameters.Add(new OleDbParameter("@resID", OleDbType.Integer));
        command1.Parameters["@resID"].Value = selection.Reserveringsnummer;

        command1.Parameters.Add(new OleDbParameter("@sID", OleDbType.Integer));
        command1.Parameters["@sID"].Value = 1;

        command1.Parameters.Add(new OleDbParameter("@aantal", OleDbType.Integer));
        command1.Parameters["@aantal"].Value = nudShower.Value;

        reader1 = command1.ExecuteReader();
        if(reader1.HasRows)
        {
            MessageBox.Show("Something went wrong!");
        }
        else
        {
            MessageBox.Show("Services updated");
        }
    }

1 个答案:

答案 0 :(得分:2)

在OleDb中,不会检查参数的名称,而是查看其位置。您需要按照它们在查询中出现的顺序将参数放在集合中。

反转INSERT查询中字段的顺序允许您以相同的顺序插入INSERT和UPDATE的参数。

if(reader.GetInt32(3) == 0)
{
    command1.CommandText = @"INSERT INTO Service_Overzicht 
                            (Aantal, OverzichtID, ServiceID) 
                            VALUES ( @aantal, @resID, @sID)";
}
else
{
    command1.CommandText = @"UPDATE Service_Overzicht
                            SET Aantal = @aantal
                            WHERE OverzichtID = @resID AND ServiceID = @sID";
}
command1.Parameters.Add("@aantal", OleDbType.Integer).Value = nudShower.Value;        
command1.Parameters.Add("@resID", OleDbType.Integer).Value = selection.Reserveringsnummer;
command1.Parameters.Add("@sID", OleDbType.Integer).Value = 1;
command1.Connection = connection;

最后,使用ExecuteNonQuery调用执行UPDATE / INSERT / DELETE查询,并查看表示已更改行数的返回值。
调用ExecuteReader也有效,因为它执行传递的查询,但它尝试构建一个OleDbDataReader来读取假定的返回记录集。
您没有要使用更新查询回读的记录,而只是一个数字。

  int rows = command1.ExecuteNonQuery();
  if(rows != 0) 
      ....success....