我尝试对存储过程执行批量调用,以在两个系统之间同步数据。我的SP有几个in参数和1个out参数。一切正常,但我似乎无法获得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();
}
}
我一直在试着试着让它工作几天,但无论我怎么做,o_val总是为零。
非常感谢任何帮助。