尝试使用where子句中使用的like语句删除记录时,Advantage数据库会引发异常

时间:2010-06-01 16:28:32

标签: advantage-database-server

以下代码显示当sql语句为:

时删除记录
select * from test where qty between 50 and 59  

但是sql语句:

select * from test where partno like 'PART/005%'

抛出异常:

Advantage.Data.Provider.AdsException: Error 5072:  Action requires read-write access to the table

如何可靠地删除带有where子句的记录?
注意:我使用的是Advantage Database v9.10.1.9,VS2008,.Net Framework 3.5和WinXP 32位

using System.IO;
using Advantage.Data.Provider;
using AdvantageClientEngine;
using NUnit.Framework;

namespace NetworkEidetics.Core.Tests.Dbf
{
  [TestFixture]
  public class AdvantageDatabaseTests
  {
    private const string DefaultConnectionString = @"data source={0};ServerType=local;TableType=ADS_CDX;LockMode=COMPATIBLE;TrimTrailingSpaces=TRUE;ShowDeleted=FALSE";
    private const string TestFilesDirectory = "./TestFiles";

    [SetUp]
    public void Setup()
    {
      const string createSql = @"CREATE TABLE [{0}] (ITEM_NO char(4), PARTNO char(20), QTY numeric(6,0), QUOTE numeric(12,4)) ";
      const string insertSql = @"INSERT INTO [{0}] (ITEM_NO, PARTNO, QTY, QUOTE) VALUES('{1}', '{2}', {3}, {4})";
      const string filename = "test.dbf";

      var connectionString = string.Format(DefaultConnectionString, TestFilesDirectory);

      using (var connection = new AdsConnection(connectionString)) {
        connection.Open();

        using (var transaction = connection.BeginTransaction()) {
          using (var command = connection.CreateCommand()) {
            command.CommandText = string.Format(createSql, filename);
            command.Transaction = transaction;
            command.ExecuteNonQuery();
          }

          transaction.Commit();
        }

        using (var transaction = connection.BeginTransaction()) {
          for (var i = 0; i < 1000; ++i) {
            using (var command = connection.CreateCommand()) {
              var itemNo = string.Format("{0}", i);
              var partNumber = string.Format("PART/{0:d4}", i);
              var quantity = i;
              var quote = i * 10;

              command.CommandText = string.Format(insertSql, filename, itemNo, partNumber, quantity, quote);
              command.Transaction = transaction;
              command.ExecuteNonQuery();
            }
          }
          transaction.Commit();
        }

        connection.Close();
      }
    }

    [TearDown]
    public void TearDown()
    {
      File.Delete("./TestFiles/test.dbf");
    }

    [Test]
    public void CanDeleteRecord()
    {
      const string sqlStatement = @"select * from test";

      Assert.AreEqual(1000, GetRecordCount(sqlStatement));
      DeleteRecord(sqlStatement, 3);
      Assert.AreEqual(999, GetRecordCount(sqlStatement));
    }

    [Test]
    public void CanDeleteRecordBetween()
    {
      const string sqlStatement = @"select * from test where qty between 50 and 59";

      Assert.AreEqual(10, GetRecordCount(sqlStatement));
      DeleteRecord(sqlStatement, 3);
      Assert.AreEqual(9, GetRecordCount(sqlStatement));
    }

    [Test]
    public void CanDeleteRecordWithLike()
    {
      const string sqlStatement = @"select * from test where partno like 'PART/005%'";

      Assert.AreEqual(10, GetRecordCount(sqlStatement));
      DeleteRecord(sqlStatement, 3);
      Assert.AreEqual(9, GetRecordCount(sqlStatement));
    }

    public int GetRecordCount(string sqlStatement)
    {
      var connectionString = string.Format(DefaultConnectionString, TestFilesDirectory);
      using (var connection = new AdsConnection(connectionString)) {
        connection.Open();

        using (var command = connection.CreateCommand()) {
          command.CommandText = sqlStatement;
          var reader = command.ExecuteExtendedReader();
          return reader.GetRecordCount(AdsExtendedReader.FilterOption.RespectFilters);
        }
      }
    }

    public void DeleteRecord(string sqlStatement, int rowIndex)
    {
      var connectionString = string.Format(DefaultConnectionString, TestFilesDirectory);
      using (var connection = new AdsConnection(connectionString)) {
        connection.Open();

        using (var command = connection.CreateCommand()) {
          command.CommandText = sqlStatement;

          var reader = command.ExecuteExtendedReader();

          reader.GotoBOF();
          reader.Read();

          if (rowIndex != 0) {
            ACE.AdsSkip(reader.AdsActiveHandle, rowIndex);
          }
          reader.DeleteRecord();
        }

        connection.Close();
      }
    }
  }
}

1 个答案:

答案 0 :(得分:5)

LIKE导致静态游标而不是实时游标,这意味着它是一个只读数据集。要在这种情况下删除行,最好使用SQL DELETE语句。

DELETE FROM test where partno LIKE 'PART/005%'

我假设你的测试只是测试。他们正在使用一些效率相当低的机制来定位和删除行。

在评论没有关键字段后更新:

如何使用LEFT标量而不是LIKE(可能不适用于所有情况,但适用于您的示例)。如果大小始终相同,您还可以在左侧添加索引(partno,8)以提高性能:

select * from test where left(partno,8) = 'PART/005' 

然后你可以直接在这个实时结果集上使用扩展数据阅读器的删除功能(没有必须跳过)。

在Alex的ROWID评论后更新 我不知道我们的ROWID来自基表,即使是静态游标也是如此。 Alex的评论是您问题的解决方案。第一:

SELECT t.*, t.rowid FROM test t WHERE x LIKE 'PART/005%'

然后:

DELETE FROM test WHERE rowid = :thisid