我有数千行的视觉狐狸专业数据库。我正在使用oledb从fox pro获取数据并将其导出(在进行一些计算和填充之后)到sql server。我已经使用数据集一次填充2-3个数据表(相关表)。
第一个问题是内存使用非常高,因为数据集很大。我想减少内存占用。对此提出了任何建议。
所以我决定一次获取几行。如何使用oledb命令获取行,以便我可以获取例如1-20然后20-40等
答案 0 :(得分:1)
string queryString = "SELECT OrderID, CustomerID FROM Orders WHERE ORDERID >= @StartOrderID AND ORDERID <= @EndOrderID";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add(new OleDbParameter("@StartOrderID", "PASS THE VALUE HERE"));
command.Parameters.Add(new OleDbParameter("@EndOrderID", "PASS THE VALUE HERE"));
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Retrieve records here
}
reader.Close();
}
答案 1 :(得分:1)
根据所有评论和HatSoft的代码,我认为您正在寻找。这是伪代码,这意味着它不会编译,但应该让你很好地了解从这里去的地方。
int NumberOfRecordsToRetrieve = 10000;
int StartRecordNumber = 1;
bool EndOfFile = false;
string queryString = "SELECT OrderID, CustomerID FROM Orders WHERE RECNO() BETWEEN @StartRecordNumber AND @EndRecordNumber";
While (!EndOfFile)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add(new OleDbParameter("@StartRecordNumber", StartRecordNumber));
command.Parameters.Add(new OleDbParameter("@EndRecordNumber", StartRecordNumber + NumberOfRecordsToRetrieve));
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
EndOfFile = true;
while (reader.Read())
{
EndOfFile = false
//Retrieve records here and do whatever process you wish to do
}
reader.Close();
StartRecordNumber += NumberOfRecordsToRetrieve;
}
}