Hi guys I have the follow code
string strCadenaConexion = @"X://Personal//Bases";
DBHelperVFPDBF db = new DBHelperVFPDBF(strCadenaConexion);
foreach (var item in lista)
{
string valuesString = $"INSERT INTO relojjcopiaLuis" +
$" (id, numdoc, fecha, maquina, tipo, veces, sucursal, transf, forma, audito, newfld)" +
$" VALUES ('{item.id}',{item.numdoc}," +
$" DATETIME({item.fecha.Year},{item.fecha.Month},"+
$" {item.fecha.Day},{item.fecha.Hour},{item.fecha.Minute},{item.fecha.Second})," +
$" '{item.maquina}','{item.tipo}'," +
$" {item.veces},'{item.sucursal}',DATETIME({item.transf.Year},{item.transf.Month},"+
$" {item.transf.Day},0,0,0)," +
$" '{item.forma}','{item.audito}','{item.newfld}');";
if (db.ProbarConexion())
{
try
{
using (OleDbCommand command = new OleDbCommand(valuesString))
{
db.EjecutarQuery(command);
Log.Error($"Se pudo guardar la informacion {item.numdoc} {item.id} ");
}
}
catch (Exception e)
{
Log.Error("No se pudo guardar la informacion " + e.Message);
}
}
await Task.Delay(delay/lista.Count);
}
log.Information($"Se insertaron {lista.Count} nuevas fichadas al Fox")
If we check in the files managed by VFP, in my case, I refer to my table:
relojj.dbf
relojj.ftp
relojj.cdx
If I delete the relojj.cdx file, I have no issues with inserting. However, with the relojj.cdx file, which contains the indices, I get the following error:
Error building key for index "x:\personal\bases\relojjcopiaLuis.cdx" tag "Fecha".
The problem is in the cdx file, where the table indexes are stored. In this case, I have an index called "Fecha" with an expression that looks like this: TTOC(date) + STR(numdoc, 8, 0). This index and expression were created in Visual Fox Pro and are necessary. I cannot delete them. This C# project inserts data every hour, so it's not an option to insert data after deleting the .cdx file and then recreating the index as it is now. What's interesting and important to note is that if I remove this expression and leave a simple ASC as expression, there are no issues.
I read in some forums that it might be related to the number of characters, and in another, it might be due to the index reordering not completing because of date formats. However, the table shows the data as it was directly inserted from Visual Fox Pro. The latter makes more sense because if I remove the expression, as I mentioned earlier, there are no issues, as you can see in the field, the date is "18/10/2023 20:34:15".
Does anyone have any ideas on how to correct this?
How can I insert data to dbf with cdx create and having a tag with expression
The filenames you supplied does not match with the filename in the error message. That makes me think that you are skipping some details about your files. You are saying "relojj" but in code using "relojjcopiaLuis".
Anyway, could you try changing the code like this: