I am trying to access a MySql database in a C# console application, just to get it to print a couple of values to make sure it connects. It keeps giving me a "the name "reader" does not exist in the current context" error.
This is the code I'm using...
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MySql.Data.MySqlClient; using System.Data; namespace sql_test { class Program { static void Main(string[] args) { string connectionString = "Server=localhost;" + "Database=DBname;" + "User ID=DBID;" + "Password=DBpass;" + "Pooling=false;"; IDbConnection dbcon; dbcon = new MySqlConnection(connectionString); try { dbcon.Open(); } catch(Exception ex1) { Console.WriteLine(ex1.Message); } IDbCommand dbcmd = dbcon.CreateCommand(); string sql = "SELECT RoomId, RoomName " + "FROM Resources"; dbcmd.CommandText = sql; try { IDataReader reader = dbcmd.ExecuteReader(); } catch (InvalidOperationException ex2) { Console.WriteLine(ex2.Message); } while (reader.Read()) { string roomID = (string)reader["RoomId"]; string roomName = (string)reader["RoomName"]; Console.WriteLine("Name: " + roomID + " " + roomName); } // clean up reader.Close(); reader = null; dbcmd.Dispose(); dbcmd = null; dbcon.Close(); dbcon = null; } } } obviously i have changed the mysql login password and server names etc. but other wise its all the same...
the only "reader" that doesn't have the error is the one after IDataReader about line 39.
Any help would be much appreciated.