0

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.

0

1 Answer 1

4

SOURCE OF THE PROBLEM:

reader variable is outside the variable scope for the next statements after the try statement.

1. Simple, but bad fix:

Declare reader outside the block:

 IDataReader reader = null; try { reader = dbcmd.ExecuteReader(); } catch (InvalidOperationException ex2) { Console.WriteLine(ex2.Message); } 

2. Recommended solution:

Previous solution may lead to NullReferenceException if something happens, so better put your entire connection and processing code inside the try statement and your cleanup inside the finally block:

IDataReader reader = null; IConnection dbconn = null; try { dbcon = ... reader = dbcmd.ExecuteReader(); while (reader.Read()) { string roomID = (string)reader["RoomId"]; string roomName = (string)reader["RoomName"]; Console.WriteLine("Name: " + roomID + " " + roomName); } } catch (InvalidOperationException ex2) { Console.WriteLine(ex2.Message); } finally { // clean up if (reader != null) reader.Close(); if (dbcmd != null) dbcmd.Dispose(); // ... } 

There is no need to set your variables to null in your dispose code - in this case (for local variables) it won't change anything.

3. Alternative solution:

There is also C# using statement, that simplifies resource disposal:

 using (IConnection connection = new Connection...) { using (ICommand command = new Command...) { // Do processing } } 

But it won't help with error handling - you will still have to put your code inside the try catch block:

try { using (IConnection conn = ...) { } } catch (Exception exc) // !!! Catching Exception and not something more specific may become a source of problems { // Handle errors } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.