I use a universal interface-based data provider. Reading data that was created in MS Access is performed without problems. The data that was added through the program, for some reason is not read. To be more precise, they are partially read. For example, you can get the number of rows, but reading the value by the index does not work, as if these lines are not in the database. Method to add a string:

public int AppendRow(string tableName) { IDbCommand cmd = null; IDbDataAdapter ida = null; int result = 0; try { connection.Open(); cmd = connection.CreateCommand(); cmd.CommandText = "INSERT INTO " + tableName + " DEFAULT VALUES"; ida = PrvDataAdapter(); ida.InsertCommand = cmd; result = ida.InsertCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { if (connection.State == ConnectionState.Open) connection.Close(); } cmd.Dispose(); return result; } IDbDataAdapter PrvDataAdapter() { IDbDataAdapter ida = null; switch (_provider) { case Provider.OleDb: ida = new OleDbDataAdapter(); break; case Provider.SqlClient: ida = new SqlDataAdapter(); break; } return ida; } 

Method for reading integer values ​​(ID field):

 public int GetIntValue(string tableName, int indexColumn, int indexRow) { IDbCommand cmd = null; IDataReader idr = null; int value = 0; try { connection.Open(); cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM " + tableName; idr = cmd.ExecuteReader(); DataTable table = new DataTable(); table.Load(idr); DataColumn column = table.Columns[indexColumn]; DataRow row = table.Rows[indexRow]; if (table.Columns[indexColumn].DataType == typeof(int)) value = (int)row[column]; idr.Close(); table.Dispose(); column.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { if (connection.State == ConnectionState.Open) connection.Close(); } cmd.Dispose(); idr.Dispose(); return value; } 

Apparently, the method of adding a record by default creates incorrect data. The question is, what is not done?

  • In the ACCESS itself, did you check for software data inserted? What does the phrase "as if these lines are not in the database" mean? Does an OutOfRangeException exception occur? Or what is the program's reaction to an attempt to read the lines inserted programmatically? - kodv
  • Yes, of course, an entry is added to the Access database. No exceptions, not even a hint of a mistake. I checked at the time of debugging, the number of rows is correct, but the cell value is not displayed. And it does not matter for which column. - vite apprentice
  • "Not displayed" - this is how? In the above example, you read the integer value - it cannot be absent. What does the function return if the lines are added programmatically? What value do you see for this string in ACCESS? Then you add DEFAULT VALUES ... - kodv
  • For example, the line under the index (2) was added programmatically. Lines with an index (0-1) were added through the MS Access 2016 interface (the database is saved in the format of 2000-2003 with the extension * .mdb). When you pass under the debugger, the value of the table.Rows property on the index (2) shows the index value (1), although the Count property clearly records the total number of rows "3". - vite apprentice
  • It seems I understood the reason, the value of the added string was recorded in the index (0). With what it can be connected? Need to explicitly specify an index? - vite apprentice

1 answer 1

Apparently, this is the optimal solution:

 public int AppendRow(string tableName) { IDbCommand cmd = null; int result = 0; try { connection.Open(); cmd = connection.CreateCommand(); cmd.CommandText = "INSERT INTO " + tableName + " DEFAULT VALUES"; result = cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } finally { if (connection.State == ConnectionState.Open) connection.Close(); } cmd.Dispose(); return result; }