Problem reading and inserting data in the DBF file after deleting all records

1

I'm creating a system that will read a xls file, delete the data from a dbf file, and then include all the xls records in the table.

However, this system is an intermediate system, since dbf data is used in a VB system.

The intermediate system is querying the xls , writing the data to a DataTable , it is deleting the dbf data (it is even giving "PACK" to confirm the deletion of the data) and inserts new records in the file .

I'm using the DBF Commander to check if the data is actually there. But when I open the system and I generate a query, it does not return any records and also does not allow me to insert new records.

I am putting the deletion code, because I have verified that even without inserting a record by my system, leaving dbf clean, I can not include new records

private void btnDeletar_Click(object sender, EventArgs e)
{
    // Pergunta se deseja continuar
    if (clsGeneric.MsgDialog("Você está prestes a deletar a tabela. Deseja continuar?", "Cuidado!", clsGeneric.DialogType.Question) == DialogResult.Yes)
    {
        // Instâncias
        string vStrFileTree;
        string vSql;
        OleDbConnection conn;
        OleDbCommand com;
        vStrFileTree = edtPathDBF.Text;

        // Busca do diretório
        string strDirectoryPath = Path.GetDirectoryName(vStrFileTree);
        string strTable = vStrFileTree.Replace(strDirectoryPath, "");
        strTable = strTable.Replace("\", "");

        // Conexão para a alterar a flag de "registro deletado" do DBF [Não Exclui os dados]
        conn = new OleDbConnection(clsConnection.strConnectDBF(strDirectoryPath, "EXCLUSVE=YES;"));
        conn.Open();
        try
        {
            com = new OleDbCommand();
            com.Connection = conn;
            vSql = "DELETE FROM " + strTable;

            com.CommandText = vSql;
            com.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
            conn = null;
            com = null;

        }

        // Regerando as conexões, fazendo o PACK do arquivo DBF [PACK - Exclusão Permanente dos dados deletados]
        conn = new OleDbConnection(clsConnection.strConnectDBF(strDirectoryPath, "EXCLUSVE=YES;"));
        conn.Open();
        try
        {
            com = new OleDbCommand();
            com.Connection = conn;

            com.CommandText = "PACK " + strTable;
            com.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
            conn = null;
            com = null;
        }
    }
    getDBFTable(grdImpDBF, edtPathDBF, false);
}

The following is the string string code:

public static string strConnectDBF(string Path, string Extended = "")
{
    vStrConn = "Provider=VFPOLEDB.1;";
    vStrConn = vStrConn + "Data Source=" + Path + ";";
    if (Extended != "")
    {
        vStrConn = vStrConn + Extended;
    }

    return vStrConn;
}
    
asked by anonymous 04.08.2016 / 21:58

1 answer

1

I found out what the problem was. The problem was that by changing the .DBF file, I was not reindexing the table in the .CDX file, so the main system could not find the information in the .DBF file, just returning the already indexed records in .CDX.

I solved the problem by creating a routine that excluded the .CDX file, because the main system when querying the data already created the file if it did not exist.

    
05.08.2016 / 19:39