将多个对象插入MySQL

时间:2012-05-09 21:26:15

标签: c# mysql

是否有其他方法可以将多个对象插入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))

BEGIN INSERT INTO Subject_has_ProjectsSubject_IdProjects_IdUsed)VALUES(param_SubjectId,param_ProjectId,param_Used); END

3 个答案:

答案 0 :(得分:2)

很少有改进的地方:

  • 在循环外只打开一次连接(无需关闭 使用)
  • 创建命令,在循环前一次分配连接
  • 使用虚拟值
  • 在循环之前创建所有参数
  • 仅分配循环内的值并调用ExecuteScalar()

using(MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr)) { conn.Open(); 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",0 ); cmd.Parameters.AddWithValue("@param_ProjectId", 0); cmd.Parameters.AddWithValue("@param_Used", false ); foreach(List<object> sub in listSubject) { cmd.Parameters["@param_SubjectId"].Value = Convert.ToInt32(sub[0]) ; cmd.Parameters["@param_ProjectId"].Value = Convert.ToInt32(sub[1]); cmd.Parameters["@param_Used"].Value = Convert.ToBoolean(sub[2]); Id = (Int64)cmd.ExecuteScalar(); } }

答案 1 :(得分:1)

你可以试试。在foreach循环外打开连接。这将节省每次循环打开和关闭连接的时间。这将提高性能。

Int64 Id = 0;
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr))
{
    //Goes through the List<object>
    conn.Open();
    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]) );
        Id = (Int64)cmd.ExecuteScalar();
    }
     conn.Close();
}

How bad is opening and closing a SQL connection for several times? What is the exact effect?

答案 2 :(得分:0)

您是否考虑过使用单笔交易围绕这些来电?