.net mysql批处理存储过程

v1l68za4  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(230)

我正在尝试对存储过程执行批处理调用,以便在两个系统之间同步数据。我的sp有几个in参数和一个out参数。一切都很好,只是我好像拿不出参数。
我创建了一个简单的示例来概述我的问题。
我的存储过程如下所示。

CREATE PROCEDURE `sampleProc`(IN i_val INT, OUT o_val INT)
BEGIN
    SELECT i_val + i_val INTO o_val;
END

我的.net代码

class TestingSP
{
    public static void Test()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("i_val", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("o_val", typeof(System.Int32)));

        for (int x = 1; x <= 100; x++)
        {
            DataRow dr = dt.NewRow();
            dr["i_val"] = x;
            dr["o_val"] = 0;

            dt.Rows.Add(dr);
        }

        MySqlCommand command = new MySqlCommand();

        command.CommandText = "sampleProc";
        command.CommandType = CommandType.StoredProcedure;
        command.UpdatedRowSource = UpdateRowSource.OutputParameters;

        command.Parameters.Add("?i_val", MySqlDbType.Int32).SourceColumn = "i_val";

        MySqlParameter output = new MySqlParameter();
        output.ParameterName = "?o_val";
        output.MySqlDbType = MySqlDbType.Int32;
        output.Direction = ParameterDirection.Output;
        output.SourceColumn = "o_val";

        command.Parameters.Add(output);

        MySqlConnectionStringBuilder conBuilder = new MySqlConnectionStringBuilder();

        conBuilder.Server = "myserver";
        conBuilder.UserID = "root";
        conBuilder.Password = "password";
        conBuilder.Port = 3308;
        conBuilder.Database = "test_db";

        Console.WriteLine("Rows: " + dt.Rows.Count);

        using (MySqlConnection connection = new MySqlConnection(conBuilder.ConnectionString))
        {
            connection.Open();
            command.Connection = connection;

            using (MySqlDataAdapter da = new MySqlDataAdapter())
            {
                da.AcceptChangesDuringUpdate = true;
                da.ContinueUpdateOnError = true;
                da.UpdateCommand = command;
                da.UpdateBatchSize = 50;
                da.Update(dt);

                foreach(var c  in dt.GetErrors())
                {
                    Console.WriteLine("Err: " + c.RowError);
                }

                foreach(DataRow row in dt.Rows)
                {
                    Console.WriteLine("{0}: {1}", row["i_val"], row["o_val"]);
                }
            }
        }

        command.Dispose();

        Console.WriteLine("Done...");

        Console.ReadLine();
    }
}

我已经抓挠了我的头好几天,试图让这个工作,但无论我尝试什么,值总是零。
任何帮助都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题