Need to iterate through SQL table rows, one at a time (table too big to use adapter.Fill)

前端 未结 2 1824
南笙
南笙 2021-01-02 15:40

It\'s easy enough for me to read through a small SQL Server 2005 table like this:

string cmdText = \"select * from myTable\";
SqlDataAdapter adapter = new Sq         


        
相关标签:
2条回答
  • 2021-01-02 16:18

    Just use a SqlDataReader instead.

    0 讨论(0)
  • 2021-01-02 16:30

    You should use a DataReader:

    using( var connection = new SqlConnection( "my connection string" ) ) {
        using( var command = connection.CreateCommand() ) {
            command.CommandText = "SELECT Column1, Column2, Column3 FROM myTable";
    
            connection.Open();
            using( var reader = command.ExecuteReader() ) {
                var indexOfColumn1 = reader.GetOrdinal( "Column1" );
                var indexOfColumn2 = reader.GetOrdinal( "Column2" );
                var indexOfColumn3 = reader.GetOrdinal( "Column3" );
    
                while( reader.Read() ) {
                    var value1 = reader.GetValue( indexOfColumn1 );
                    var value2 = reader.GetValue( indexOfColumn2 );
                    var value3 = reader.GetValue( indexOfColumn3 );
    
                    // now, do something what you want
                }
            }
            connection.Close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题