【问题标题】:How input a list of int pairs to a MySQL store procedure from C#?如何从 C# 将 int 对列表输入到 MySQL 存储过程?
【发布时间】:2015-04-14 11:53:29
【问题描述】:

如何从 C# 将 int 对列表输入到 MySQL 存储过程?

我有一个包含 2500 个 int 对的列表,并希望将其作为输入,以便在 MySQL 中作为两列手持。 例如。我想发送输入。

List<IntPair> input = new List<IntPair>();

struct IntPair{
    int a;
    int b;
}

【问题讨论】:

    标签: c# mysql stored-procedures


    【解决方案1】:

    MySQL 中没有 IntPair-Type。使用 2 个不同的 Int 参数

    using (MySqlConnection myConnection = new MySqlConnection("ConnString"))
    {
        myConnection.Open();
        foreach (var item in input)
        {                  
            using (MySqlCommand myCommand = new MySqlCommand("spInputData", myConnection))
            {
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@Parameter1", item.a);
                myCommand.Parameters.Add("@Parameter2", item.b);
                myCommand.ExecuteNonQuery();
            }
        }
    }
    

    【讨论】:

    • 这不是一个选项,因为这会增加开销,因为我必须对同一行进行多次搜索,而不是使用 IN 命令。我测试了不同的方法,做一个大搜索比许多小搜索快得多。如果这不正确,请说明我应该在哪些设置中更改以移除开销。
    • 您可以使用多列索引来提高性能dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
    • 一般来说不使用索引有帮助吗?当我有多个查询而不是一个查询时,它有帮助吗?
    • 当您查询多列时会有所帮助(例如 IntPair 对象中的 2 个整数值)
    • 好点!但是我仍然需要将列表发送到函数。即使没有回答问题,您仍然获得了赞成票。
    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多