错误:当前上下文中不存在

时间:2014-08-15 15:29:06

标签: c# mysql

我正在尝试访问C#控制台应用程序中的MySql数据库,只是为了让它打印几个值以确保它连接。它一直给我一个名字"读者"在当前背景下不存在"错误。

这是我使用的代码......

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;
        }
    }
}
显然我已经改变了mysql登录密码和服务器名称等,但其他方面也是如此......

唯一的#34;读者"没有错误的是IDataReader关于第39行的错误。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

问题来源:

对于try语句之后的下一个语句,

reader变量超出variable scope

<强> 1。简单但不好修复:

在街区外宣布读者:

        IDataReader reader = null;

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

<强> 2。推荐的解决方案:

如果发生某些事情,以前的解决方案可能会导致NullReferenceException,所以最好将整个连接和处理代码放在try语句中并清理finally块中的内容:

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();
    // ...
}

在您的配置代码中不需要将变量设置为null - 在这种情况下(对于局部变量)它不会改变任何内容。

第3。替代解决方案:

还有C#using语句,简化了资源处理:

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

但它对错误处理没有帮助 - 您仍然需要将代码放在try catch块中:

try
{
    using (IConnection conn = ...)
    {
    }
}
catch (Exception exc) // !!! Catching Exception and not something more specific may become a source of problems
{
    // Handle errors
}