c# mysql使用MySql.Data批量插入数据

c# mysql使用MySql.Data批量插入数据_第1张图片

try
            {
     
                DataTable dt = new DataTable();
                dt.Columns.Add("param_id", typeof(int));
                dt.Columns.Add("param_name", typeof(string));
                DataRow row = dt.NewRow();
                row["id"] = 2;
                row["name"] = "tom";
                dt.Rows.Add(row);
                var p = new object[] {
     
                    new MySqlParameter("@param_id", MySqlDbType.Int32, 11, "id"),
                    new MySqlParameter("@param_name", MySqlDbType.String, 255, "name"),
                };
                MySqlConnection conn = new MySqlConnection("Database=test;Data Source=localhost;User Id=root;Password=root");
                conn.Open();
                string sqls = "insert into test_table values(@param_id,@param_name)";
                string sqlInsert = @sqls;
                using (conn)
                {
     
                    MySqlTransaction trans = conn.BeginTransaction();
                    var sqlDataAdapter = new MySqlDataAdapter();
                    MySqlCommand InsertCommand = new MySqlCommand(sqlInsert, conn);
                    InsertCommand.Parameters.AddRange(p);
                    sqlDataAdapter.UpdateBatchSize = 10000;
                    sqlDataAdapter.Update(dt);
                    trans.Commit();
                }
            }
            catch (Exception e)
            {
     
                throw e;
            }
        }

你可能感兴趣的:(c# mysql使用MySql.Data批量插入数据)