如何使用 c# 中的 MySQL 存储过程将 table 作为输入发送到存储过程?我有 T-SQL 工作

How do I use a MySQL stored procedure from c# which sends a table as input to the store procedure? I have T-SQL working

在 Transact-SQL 我有存储过程的输入参数

@DataTable InputTabel READONLY,

而 "InputTabel" 定义为:

CREATE TYPE InputTabel AS TABLE
    (
        ID INT NULL,
        RESULT_INDEX INT NULL
    )

而在 C# 方面,我使用此代码来定义参数:

SqlParameter parameter = new SqlParameter( );
parameter.ParameterName = "@DataTable ";
parameter.SqlDbType = System.Data.SqlDbType.Structured;
parameter.TypeName = "InputTabel";
parameter.Value = theInputTable;

其中输入表定义为:

DataTable theInputTable = new DataTable("TheInputTableName");
theInputTable.Columns.Add( "ID", typeof( Int32 ) );
theInputTable.Columns.Add( "RESULT_INDEX", typeof( string ) );

我现在必须将这种方法转移到 MySQL,我该怎么做呢? 在 C# 端和服务器端。

很遗憾,MySql 没有实现 table 值的参数,但确实存在替代方案:

  • 使用临时 table 连接关闭后立即删除 但对于通过此连接进行的所有查询都是可见的。唯一的缺点是必须在预期查询之前执行一个额外的查询(或更多),只是为了将数据插入临时 table.
  • 使用分隔字符串 ("str1,str2,str3") 会显着降低性能,尤其是当字符串很长时。

有关这两种方法的更多信息: Create table variable in MySQL