如何将C#数据表传递给mysql的存储过程参数

时间:2019-04-29 06:29:11

标签: c# mysql stored-procedures datatable

我具有C#(ASP.NET)形式的数据表,我想将该数据表传递给mysql中的过程参数,并将该孔数据表转储到mysql中的临时表中

如何在mysql中实现它?

请让我知道!!!!

谢谢

1 个答案:

答案 0 :(得分:1)

这里是一个例子:

CREATE TYPE members_table_type AS TABLE
(
    mem_username VARCHAR(25),
    mem_firstname VARCHAR(25),
    mem_lastname VARCHAR(25)
);

CREATE STORED PROCEDURE insert_members
@members members_table_type
AS
BEGIN
    INSERT INTO dbo.members 
    (mem_username, mem_firstname, mem_lastname)
    SELECT mem_username, mem_firstname, mem_lastname FROM @members;
END


In .NET:
DataTable members = new DataTable();
members.Columns.Add(new DataColumn("mem_username", typeof(string)));
members.Columns.Add(new DataColumn("mem_firstname", typeof(string)));
members.Columns.Add(new DataColumn("mem_lastname", typeof(string)));

DataRow row = members.NewRow();
row["mem_username"] = "test";
row["mem_firstname"] = "test";
row["mem_lastname"] = "test";
members.Rows.Add(row);

SqlCommand command = new SqlCommand("insert_members");
command.Parameters.Add(members, SqlDbType.Structured);
command.ExecuteNonQuery();