在C#中處理大量數據可以使用ODBC連接來從數據庫中檢索數據。以下是一些處理大量數據的方法:
string connectionString = "your_connection_string_here";
string query = "SELECT * FROM your_table";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
using (OdbcCommand command = new OdbcCommand(query, connection))
{
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// Process each row of data here
}
}
}
}
string connectionString = "your_connection_string_here";
int pageSize = 1000;
int currentPage = 1;
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
while (true)
{
string query = $"SELECT * FROM your_table ORDER BY id OFFSET {pageSize * (currentPage - 1)} ROWS FETCH NEXT {pageSize} ROWS ONLY";
using (OdbcCommand command = new OdbcCommand(query, connection))
{
using (OdbcDataReader reader = command.ExecuteReader())
{
if (!reader.HasRows)
{
break;
}
while (reader.Read())
{
// Process each row of data here
}
}
}
currentPage++;
}
}
string connectionString = "your_connection_string_here";
string query = "SELECT * FROM your_table";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
await connection.OpenAsync();
using (OdbcCommand command = new OdbcCommand(query, connection))
{
using (OdbcDataReader reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
// Process each row of data here
}
}
}
}
通過以上方法,可以有效處理大量數據并避免內存溢出的問題。