我知道这看起来很简单,但似乎不起作用。我在MsAccess数据库中有一个表是空的,但它在某些时候确实有一些记录,当我执行DataTable.NewRow时,它返回一个新行,其中objectid字段为0(autoincerement字段)。在我将这个新行提交到数据库后,它有objectid 84(或者它认为下一个数字应该是什么),但我需要在提交之前有正确的objectid值,因为它在连接表中使用。如果我的数据表上至少有1条记录并且执行datatable.NewRow,那么对于新行的objectid将是正确的84.这是我用来获取数据的代码:
string sSQL = "Select * from <tableName>"
IDbDataAdapter dbAdapter = GetDataAdapter(sSQL);
dbAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
if (DbConnection.State == ConnectionState.Closed)
DbConnection.Open();
try
{
DataAdapterFill(dbAdapter, dataSet, sTableName);
}
catch (OleDbException oledbEx)
{
if (oledbEx.Message.Contains("No value given for one or more required parameters"))
throw new Exception(string.Format("Where clause {0} contains correct fields for table {1}", sWhereClause, sTableName), oledbEx);
else
throw oledbEx;
}
catch
{
DbConnection.Close();
((IDisposable)dbAdapter).Dispose();
throw;
}
dbAdapter.TableMappings.Add(sTableName, sTableName);
dataTable = dataSet.Tables[0];