除了此处显示的方式之外,还有其他方法可以将多个对象插入 MySQL 数据库。这可行,但需要时间来执行。
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr))
{
//Goes thrue the List<object>
foreach(List<object> sub in listSubject)
{
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = "CALL stp_InsertSubject(@param_SubjectId, @param_ProjectId, @param_Used);";
cmd.Parameters.AddWithValue("@param_SubjectId",Convert.ToInt32(sub[0]) );
cmd.Parameters.AddWithValue("@param_ProjectId", Convert.ToInt32(sub[1]));
cmd.Parameters.AddWithValue("@param_Used", Convert.ToBoolean(sub[2]) );
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
我的存储过程:
CREATE DEFINER=`mortenstarck`@`%` PROCEDURE `stp_InsertSubject`(param_SubjectId int(45), param_ProjectId int(45), param_Used tinyint(1))
开始插入Subject_has_Projects
( Subject_Id
, Projects_Id
, Used
) 值 (param_SubjectId, param_ProjectId, param_Used); 结尾