Parameters do not pass to a MySql stored procedure from F#

0

I'd like to invoke a stored procedure with parameters from F#, but it results in exception "MySql.Data.MySqlClient.MySqlException (0x80004005): Incorrect number of arguments for PROCEDURE test.GetValue; expected 1, got 0".

A similar code in C# works perfectly.

This is .NetCoreApp3.1, FSharp.Core/4.7.0 and MySql.Data/8.0.20. MySql server is 8.0.20, OS is Ubuntu 18.04.4 LTS.

Question: Am I doing sth. wrong or is it a bug?

NOTE: If I remove the parameter from stored procedure and hard-code it in its SELECT query, it works fine in F#. Also, when debugging, I can see the parameters perfectly in place before the call.

F# code that results in exception:

module test_mysql_fsharp.main

open System
open System.Data
open MySql.Data.MySqlClient

[<EntryPoint>]
let main argv =
    Console.WriteLine("Hello from test_mysql_fsharp !");
    let execStoredProc cs storedProcedureName storedProcedureParameters =
        use mySqlConnection = new MySqlConnection(cs)
        use command = new MySqlCommand(storedProcedureName, mySqlConnection)
        command.CommandType = CommandType.StoredProcedure |> ignore
        storedProcedureParameters |> List.iter(fun par -> command.Parameters.AddWithValue(par) |> ignore)
        mySqlConnection.Open()
        let dataTable = new DataTable()
        dataTable.Load(command.ExecuteReader())
        dataTable
    let cs = "server=127.0.0.1;port=3306;database=test;Uid=***;Pwd=***;"
    execStoredProc cs "GetValue" [("par0","KA")] |> ignore
    0 

The similar code in C# works perfectly:

using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;

namespace test_mysql_csharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello from test_mysql_csharp !");
            var cs = "server=127.0.0.1;port=3306;database=test;Uid=***;Pwd=***;";
            var sp = "GetValue";

            var ps = new List<MySqlParameter>();
            var p = new MySqlParameter("par0", "KA");
            ps.Add(p);
            var b = ExecStoredProc(cs, sp, ps.ToArray());
        }

        private static System.Data.DataTable ExecStoredProc(string cs, string spName,
            params MySqlParameter[] spParams)
        {
            using MySqlConnection connection = new MySqlConnection(cs);
            using MySqlCommand command = new MySqlCommand(spName, connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddRange(spParams);
            connection.Open();
            DataTable dt = new DataTable();
            dt.Load(command.ExecuteReader());
            return dt;
        }
    }
}

A few lines to recreate test database and table:

CREATE SCHEMA `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci ;
use test;
CREATE TABLE `test`.`test` (
  `k` VARCHAR(64) NOT NULL,
  `v` VARCHAR(64) NULL,
  PRIMARY KEY (`k`));
INSERT INTO test.test (k,v) values ('KA','VA');
DELIMITER $$
CREATE PROCEDURE `GetValue`(par0 varchar(64))
BEGIN
select * from test.test where k = par0;
END$$
DELIMITER ;

Thank you in advance -

f#
.net-core-3.1

1 Answer

0

Simple: What does THIS do?

command.CommandType = CommandType.StoredProcedure |> ignore

It just compared 2 values.

Should be:

command.CommandType <- CommandType.StoredProcedure |> ignore

User contributions licensed under CC BY-SA 3.0