MySqlConnector 没有 ExecuteScalar、ExecuteNonQuery、ExecuteDataset

问题描述 投票:0回答:1

我们正在尝试从 MySql.Data 迁移到 MySqlConnector 包。 安装包 MySqlConnector Link 后出现此错误:“MySqlHelper”不包含“ExecuteNonQuery”的定义。

看起来 MySqlConnector 没有预定义方法 ExecuteScalar、ExecuteNonQuery、ExecuteDataset。

是否有任何解决方法或任何方法可以为此方法编写包装器。

        public DataSet ExecuteDataSet()
        {
            MySqlConnection connection = new MySqlConnection(ConnectionString);
            connection.Open();
            try
            {
                return MySqlHelper.ExecuteDataset(connection, CommandTextForMySqlHelper, ParameterArrayForMySqlHelper);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }
        }
c# ado.net mysql-connector
1个回答
0
投票

MySqlHelper 不是 MySqlConnector 包的一部分。自己创建方法:

public static DataSet ExecuteDataset(
    MySqlConnection connection,
    string commandText,
    params MySqlParameter[] commandParameters)
{
    MySqlCommand selectCommand = new MySqlCommand();
    selectCommand.Connection = connection;
    selectCommand.CommandText = commandText;
    selectCommand.CommandType = CommandType.Text;
    if (commandParameters != null)
    {
        foreach (MySqlParameter commandParameter in commandParameters)
            selectCommand.Parameters.Add(commandParameter);
    }
    MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(selectCommand);
    DataSet ds = new DataSet();
    mySqlDataAdapter.Fill(ds);
    selectCommand.Parameters.Clear();
    return ds;
}

用途:

using var connection = new MySqlConnection(ConnectionString);
DataSet dataSet = ExecuteDataset(connection, CommandTextForMySqlHelper, ParameterArrayForMySqlHelper);
// no need to open or close the connection, it's opened and closed in Fill
© www.soinside.com 2019 - 2024. All rights reserved.