looking for a brokerage account or IRA... click here Add To Favorites
return to index 

Looping Through a SQL Result in C#

The following C# structure will loop through a sql query row by row.

SqlConnection sql;
SqlCommand myCommand;
SqlDataReader rdr;
string query="";
int rowid;

            sql = new SqlConnection("SERVER=.; DATABASE=TestDB;Trusted_Connection=Yes;");
            query = "SELECT * FROM Test";
            myCommand = new SqlCommand(query, sql);
            myCommand.CommandType = CommandType.Text;
            try
            {
                sql.Open();
                rdr = myCommand.ExecuteReader();
                while (rdr.Read())
                {
                             rowid= int.Parse((string)rdr.GetSqlString(rdr.GetOrdinal("RowID")));
                }
                sql.Close();
                            
            }
            catch (Exception e)
            {
                logger.Error(e.ToString());
            }

Additional Interesting Articles

Automatically Redirect People To Different Landing Pages in PHP
Pearson Coefficient
C# DataSet v DataReader
PHP Cookie And Authentication
Check For Value In Column In Multiple Table SQL
SQL Check Existence of Table or Temp Table

©2008 AndrewKimball.com