【问题标题】:Use MySqlDataReader in C#在 C# 中使用 MySqlDataReader
【发布时间】:2017-07-22 05:24:44
【问题描述】:

我正在开发一个使用 DAO 模式的应用程序。 MySql 中的连接位于我的 DAOFactory-Class 中:

namespace GSB
{
    class DAOFactory
    {
        private MySqlConnection connectionBDD;

       public DAOFactory()
        {
            this.InitConnexion();
        }

        public void InitConnexion()
        {

            string connexion = "SERVER=127.0.0.1; DATABASE=gsb_c#; UID=root; PASSWORD=root";
            this.connectionBDD = new MySqlConnection(connexion);
        }

        private bool OpenConnection()
        {
            try
            {
                connectionBDD.Open();
                return true;
            }

            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Cannot connect to server");
                        break;

                    case 1045:
                        Console.WriteLine("Invalid UserName/Password");
                        break;
                }

                return false;
            }
        }

        public void deconnexion()
        {
            this.connectionBDD.Close();
        }

        public void execSqlRead(String req)
        {  //à remplir 
            this.OpenConnection();
            MySqlCommand cmd = this.connectionBDD.CreateCommand();
            MySqlDataReader myReader;
            myReader= cmd.ExecuteReader();  //stop here
            try
            {
                while (myReader.Read())
                {
                    Console.WriteLine(myReader.GetString(0));
                }
            }
            finally
            {
                Console.WriteLine("Yolo");
                this.deconnexion();
            }

            //return myReader;
        }

        public void execSqlWrite(String req)
        {
            this.OpenConnection();
            MySqlCommand cmd = this.connectionBDD.CreateCommand();
            //Création d'une commande SQL en fonction de l'objet connection
            cmd.CommandText = req; //on entre la req sql
            cmd.ExecuteNonQuery(); //on ex la req
            this.deconnexion();
        }

    }
}

目前我的execSqlWrite 方法正在工作,但我的exexSqlRead 不是因为当我想执行我的阅读器时它已经停止了。 我不知道如何解决这个问题,我只想从我选择的请求中获取结果。

我该怎么做?

【问题讨论】:

  • 你的读者需要一个命令来执行,你永远不会在你的读者中为cmd.CommandText分配任何东西,我假设SELECT语句是你作为req传递的,所以你需要@987654327 @

标签: c# mysql visual-studio dao


【解决方案1】:

如果您仔细查看 Writer 代码,您将看到该命令接收命令字符串。阅读器代码中缺少此行

public void execSqlRead(String req)
{  
    this.OpenConnection();
    MySqlCommand cmd = this.connectionBDD.CreateCommand();
    cmd.CommandText = req;
    MySqlDataReader myReader;
    myReader= cmd.ExecuteReader();  //stop here
    try
    {
        while (myReader.Read())
        {
            Console.WriteLine(myReader.GetString(0));
        }
    }
    finally
    {
        Console.WriteLine("Yolo");
        this.deconnexion();
    }

    //return myReader;
}

【讨论】:

  • 我在我的代码中添加了缺少的行,但应用程序仍然停止并出现相同的错误 =“MySql.Data.dll 中发生类型为 'System.InvalidOperationException' 的未处理异常”
  • 如果您查看 InnerException 消息,您应该会看到更详细的错误消息。顺便说一句,错误可能只是在命令文本中,但您应该将其添加到您的问题中。请edit它。
  • 我认为这可能会有所帮助:“ GSB.vshost.exe 错误:0:无法连接到任何指定的 MySQL 主机。抛出异常:MySql 中的“MySql.Data.MySqlClient.MySqlException” .Data.dll 抛出异常:MySql.Data.dll 中的“System.InvalidOperationException””
  • 那么问题出在连接字符串上。 (或者在服务器上为 PC 127.0.0.1 设置的权限中,我会尝试使用 localhost 代替
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
相关资源
最近更新 更多