📜  c# insert from with bind array - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:00.445000             🧑  作者: Mango

代码示例1
using(var connection = new MySqlConnection("your connection string"))
{
    using(var command = new MySqlCommand("INSERT INTO programs (name, id) VALUES (?name, ?id)", connection))
    {
        var nameParameter = new MySqlParameter("name");
        var idParameter = new MySqlParameter("id");

        command.Parameters.Add(nameParameter);
        command.Parameters.Add(idParameter);

        connection.Open();

        for(int i = 0; i < name.Length; i++)
        {
             nameParameter.Value = name[i];
             idParameter.Value = id[i];

             command.ExecuteNonQuery();             
        }

        connection.Close(); //Dispose being called by the using should close connection, but it doesn't hurt to close it here/sooner either.         
    }
}