C#MySQL ExecuteReader

时间:2017-05-02 14:23:38

标签: c# mysql executereader connection-leaks

我的C#项目有问题。我使用MySQL数据库,我使用MySQL网站的MySQL连接器驱动程序,但我有光标和连接问题。实际上,Visual Studio说从第二个过程中读取数据是不可能的,因为游标已经打开但我在新程序调用之前关闭了游标。

这是我的代码:

static public Data loadData()
{
    Data database = new Data();
    myConnexion.Open();

    /// <summary>
    ///     Loading of the categories
    /// </summary> 
    MySqlCommand command = new MySqlCommand("getCategory", myConnexion);
    command.CommandType = System.Data.CommandType.StoredProcedure;
    MySqlDataReader cursor = command.ExecuteReader();

    while (cursor.Read())
    {
        int id = Convert.ToInt32(cursor["id"]);
        string categoryName = Convert.ToString(cursor["name"]);

        Category category = new Category(id, categoryName);
        database.addCategory(category);
    }
    cursor.Close();

    /// <summary>
    ///     Loading of the projects
    /// </summary>
    command = new MySqlCommand("getProject", myConnexion);
    command.CommandType = System.Data.CommandType.StoredProcedure;
    cursor = command.ExecuteReader();

    while (cursor.Read())
    {
        int idProject = Convert.ToInt32(cursor["id"]);
        string name = Convert.ToString(cursor["name"]);
        int idCategory = Convert.ToInt32(cursor["idCategory"]);

        Category category = database.getCategories()[idCategory];
        Project project = new Project(idProject, name, category);
        Link.addProject(project.getName(), category);
    }
    cursor.Close();

    myConnexion.Close();
    return database;
}

这是我启动程序时Visual Studio的错误消息:

1 个答案:

答案 0 :(得分:1)

您可以尝试将DataReader转换为使用块,该块应该关闭并处理datareader。

            <!DOCTYPE html>
            <html>
            <head>
                <script>
                function onDeviceReady() {
                    if (window.cordova.logger) {
                        window.cordova.logger.__onDeviceReady();
                    }
                }
                function init() {
                    document.addEventListener('deviceready', onDeviceReady, true);
                }
                </script>
                <meta charset="utf-8">
                <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
                <title></title>
                <link href="lib/ionic/css/ionic.css" rel="stylesheet">
                <link href="css/style.css" rel="stylesheet">
                <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
                <link href="css/ionic.app.css" rel="stylesheet">
                -->
                <!-- ionic/angularjs js -->
                <script src="lib/ionic/js/ionic.bundle.js"></script>
                <!-- cordova script (this will be a 404 during development) -->
                <script src="cordova.js"></script>
                <!-- your app's js -->
                <script src="js/app.js"></script>
                <script src="js/factories/DeviceFactory.js"></script>
                <script src="js/controllers/HomeController.js"></script>
                <script src="js/controllers/DeviceController.js"></script>
            </head>
            <body ng-app="starter" onload="init()">
                <script>
                console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
                </script>
                <ion-nav-view></ion-nav-view>
            </body>

            </html>