Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不执行我的MySql查询 我的C代码有点问题(通常是C++的DEV,所以我只知道C的基本知识)_C#_Mysql - Fatal编程技术网

C# 不执行我的MySql查询 我的C代码有点问题(通常是C++的DEV,所以我只知道C的基本知识)

C# 不执行我的MySql查询 我的C代码有点问题(通常是C++的DEV,所以我只知道C的基本知识),c#,mysql,C#,Mysql,我想编写一个类来使用该程序执行SQL查询,但它什么也不做,我也没有从中得到任何错误 我编写的MySqlConnectionHandler类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using System.Windows.Forms; name

我想编写一个类来使用该程序执行SQL查询,但它什么也不做,我也没有从中得到任何错误

我编写的MySqlConnectionHandler类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace WS_Studio_tool
{
    class MySqlConnectionHandler
    {
        private MySqlConnection connection;
        private string ConnectionString;

        public MySqlConnectionHandler()
        {
            try
            {
                ConnectionString = "SERVER=localhost;" +
                                   "DATABASE=ws_creator_beta;" +
                                   "UID=root;" +
                                   "PASSWORD=AF362GL!";
                connection = new MySqlConnection(ConnectionString);
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "MySql Fehler!");
            }
        }
        public bool InsertRow(string SQL_Query)
        {
            try
            {
                 MySqlCommand command = connection.CreateCommand();
                 command.CommandText = SQL_Query;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "MySql Fehler!");
            }
            return true;
        }


    }
}
我的函数调用(我经常使用它,以便它可以包含不必要的内容):

和我的MySql查询:

INSERT INTO user_data (username,passwd) VALUES ('asdf', 'asdf');

如果有人能快速查看一下,那就太好了,也许你能找到错误。

你没有调用command.Execute,所以你的SQL永远不会被执行。

你没有:另外,你需要显式地,所以将这些行添加到InsertRow:

connection.Open();
command.ExecuteNonQuery();
我还建议在使用完连接后立即处理它-通过在
MySqlConnectionHandler
类上实现
IDisposable
,或者-更简单-在执行查询时使用
块在
中创建连接:

using (var connection = new new MySqlConnection(ConnectionString))
{
    using (var command = connection.CreateCommand())
    { 
         command.CommandText = SQL_Query;
         connection.Open();
         command.ExecuteNonQuery();
    }
}

您是否收到任何异常或错误消息?不,运行时没有错误,正如我所说的-它只是不执行我的sqlyou-dint-put命令。ExecuteOnQuery()在InsertRow()方法中谢谢,我将尝试:)现在我收到一个运行时错误,上面写着:System.InvalidOperationException执行查询时是否出现错误?是否有任何详细信息的内部异常?您将获得异常,因为您在使用命令链接连接之前正在使用命令@user2287114 SQL查询仍然没有错误,只是我之前发布的异常-但是在正确的范围内。
using (var connection = new new MySqlConnection(ConnectionString))
{
    using (var command = connection.CreateCommand())
    { 
         command.CommandText = SQL_Query;
         connection.Open();
         command.ExecuteNonQuery();
    }
}