Problem of reading and inserting data into DBF file after deletion of all records

Asked

Viewed 110 times

1

I’m creating a system that will read a file xls, delete data from a file dbf to then include all records of the xls on the table.

However, this system is an intermediate system, because the dbf are used in a VB system.

The intermediary system is consulting the xls, recording the data in a DataTable, is deleting the data from dbf (is even giving "PACK" to confirm the data deletion) and inserts new records in the file.

I’m using DBF Commander to check if the data is really there. But when I open the system and have a query generated, it does not return any record and also does not allow me to enter new records.

I am entering the deletion code as I have checked that even without entering a record by my system, leaving the dbf clean, 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);
}

Right below is the code of string connecting:

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

    return vStrConn;
}

1 answer

1


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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.