日期:2014-05-16 浏览次数:20350 次
OleDbDataReader读取ACCESS顺序错乱:
我在ACCESS数据库表中存有2757条记录,每个记录的第一个字段为顺序号,但不知道为什么,跟踪发现,读取到第1943条记录时,我读到的是2482号数据,查看数据库,没有发现异常,并且,每次都是一样的现象!!请问这是什么原因???代码如下:
public ArrayList LoadTestData(int sampleNumber)
{
ArrayList testDataArray = new ArrayList();
int i=0;
string dbCommand = null;
string dbConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ";
OleDbDataReader myDataReader = null;
dbConnect += m_UserDBName;
dbConnect += "; ";
TestData testData = new TestData();
try
{
this.DBConnect(dbConnect);
dbCommand = string.Format( "SELECT * FROM tblTestData{0} ", sampleNumber);
DBSelect(dbCommand, ref myDataReader);
while(myDataReader.HasRows)
{
myDataReader.Read();
testData.number = myDataReader.GetInt32 (0);
testData.forceValue = myDataReader.GetDouble (1);
testData.deformationValue = myDataReader.GetDouble (2);
testData.displacementValue = myDataReader.GetDouble (3);
testData.timeValue = myDataReader.GetDouble (4);
testDataArray.Add(testData);
i++;
}
myDataReader.Close();
DBDisconnect();
}
catch
{
DBDisconnect();
}
return testDataArray;
}
private bool DBSelect(string sqlCommand, ref OleDbDataReader sqlReader)
{
try
{
oleDBCommand.CommandText = sqlCommand;
sqlReader=oleDBCommand.ExecuteReader();
}
catch(System.Data.OleDb.OleDbException msg)
{
return false;
}
return true;
}
------解决方法--------------------
没具体看你的代码...
查询的时候,排个序...
for example:
select * from student order by sno asc;
or
select * from student order by sno ;
------解决方法--------------------
SQL语句中加上Order By ID来达到排序效果。
------解决方法--------------------
加上排序字段
dbCommand = string.Format( "SELECT * FROM tblTestData{0} order by number ", sampleNumber);