使用c#将数据表插入到mysql表中

时间:2017-12-29 06:46:37

标签: c# mysql sql

我有一个mysql表,我需要使用C#更新数据表。

我使用以下代码在sql中实现了这一点:

DataTable table;
using (SqlConnection connection = new SqlConnection(connectionString))  
{
    connection.Open();
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
       {
           bulkCopy.DestinationTableName = destinationTable;
           bulkCopy.WriteToServer(table);
       }
 }  

有没有办法为mysql实现这个目的?

3 个答案:

答案 0 :(得分:0)

您可以使用MySqlBulkLoader类。

MySql网站https://dev.mysql.com/doc/connector-net/en/connector-net-programming-bulk-loader.html

提供了其用法示例

答案 1 :(得分:0)

批量插入MySQL(例如来自MS SQL的SqlBulkCopy)

这可以解决问题:

    public void Start(string tableName, List<ClsLink> linkList)
    {            
        DataTable table = new DataTable();

        // Getting datatable layout from database
        table = GetDataTableLayout(tableName);

        // Pupulate datatable
        foreach (ClsLink link in linkList)
        {
            DataRow row = table.NewRow();                
            //row["LinkURL"] = link.LinkURL;
            //row["CreateDate"] = link.CreateDate;
            //row["Titel"] = link.Titel;
            table.Rows.Add(row);
        }

        BulkInsertMySQL(table, tableName);
        // Enjoy
    } 


    public DataTable GetDataTableLayout(string tableName)
    {
        DataTable table = new DataTable();

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            connection.Open();
            // Select * is not a good thing, but in this cases is is very usefull to make the code dynamic/reusable 
            // We get the tabel layout for our DataTable
            string query = $"SELECT * FROM " + tableName + " limit 0";
            using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, connection))
            {
                adapter.Fill(table);
            };
        }

        return table;
    }

    public void BulkInsertMySQL(DataTable table, string tableName)
    {
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            connection.Open();

            using (MySqlTransaction tran = connection.BeginTransaction(IsolationLevel.Serializable))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = connection;
                    cmd.Transaction = tran;
                    cmd.CommandText = $"SELECT * FROM " + tableName + " limit 0";

                    using (MySqlDataAdapter adapter = new MySqlDataAdapter(cmd))
                    {
                        adapter.UpdateBatchSize = 10000;
                        using (MySqlCommandBuilder cb = new MySqlCommandBuilder(adapter))
                        {
                            cb.SetAllValues = true;
                            adapter.Update(table);
                            tran.Commit();
                        }
                    };
                }
            }
        }
    }

//享受

答案 2 :(得分:-1)

我发现SqlBulkCopy是一种简单的方法!

using (var bulkCopy = new SqlBulkCopy(_connection.ConnectionString, SqlBulkCopyOptions.KeepIdentity))
{
      // my DataTable column names match my SQL Column names, so I simply made this loop. However if your column names don't match, just pass in which datatable name matches the SQL column name in Column Mappings
      foreach (DataColumn col in table.Columns)
      {
          bulkCopy.ColumnMappings.Add(col.ColumnOneName, col.ColumnTwoName, col.Soon);
      }

      bulkCopy.BulkCopyTimeout = 600; // optional 
      bulkCopy.DestinationTableName = destinationTableName;
      bulkCopy.WriteToServer(table);
}