gpt4 book ai didi

c# - 写一个大数据表到mysql数据库c#

转载 作者:行者123 更新时间:2023-11-29 04:46:54 24 4
gpt4 key购买 nike

我需要一些帮助。我正在将相当大的数据记录写入远程 MySQL 数据库。记录数从400k到超过1M不等。

我做的是

  1. 从一个数据库中收集一组记录到数据表中。

  2. 然后,在我想将数据写入不同的 MySQL 数据库之前,我对数据表执行了一些处理。

  3. 选择和插入实际上非常快,但是要执行数据插入 我正在通过遍历数据表来创建“INSERT INTO...”查询字符串。这需要很长时间,并且会延迟写出数据的过程。

我知道这是一个丑陋的解决方案,但我在搜索互联网时找不到更优雅的解决方案,尽管它可能在那里。

如有任何帮助,我们将不胜感激。

最佳答案

我碰巧遇到了同样的问题,为此写了一个小辅助方法。您可以在我的博客中找到解决方案 https://randomlol.de/2014/05/29/c-datatable-to-mysql/

/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <fg@code-works.de> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Frank Gehann
* ----------------------------------------------------------------------------
*/

using System;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace DB
{
class DBHelper
{
/// <summary>
/// Creates a multivalue insert for MySQL from a given DataTable
/// </summary>
/// <param name="table">reference to the Datatable we're building our String on</param>
/// <param name="table_name">name of the table the insert is created for</param>
/// <returns>Multivalue insert String</returns>
public static String BulkInsert(ref DataTable table, String table_name)
{
try
{
StringBuilder queryBuilder = new StringBuilder();
DateTime dt;

queryBuilder.AppendFormat("INSERT INTO `{0}` (", table_name);

// more than 1 column required and 1 or more rows
if (table.Columns.Count > 1 && table.Rows.Count > 0)
{
// build all columns
queryBuilder.AppendFormat("`{0}`", table.Columns[0].ColumnName);

if (table.Columns.Count > 1)
{
for (int i = 1; i < table.Columns.Count; i++)
{
queryBuilder.AppendFormat(", `{0}` ", table.Columns[i].ColumnName);
}
}

queryBuilder.AppendFormat(") VALUES (", table_name);

// build all values for the first row
// escape String & Datetime values!
if (table.Columns[0].DataType == typeof(String))
{
queryBuilder.AppendFormat("'{0}'", MySqlHelper.EscapeString(table.Rows[0][table.Columns[0].ColumnName].ToString()));
}
else if (table.Columns[0].DataType == typeof(DateTime))
{
dt = (DateTime)table.Rows[0][table.Columns[0].ColumnName];
queryBuilder.AppendFormat("'{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));
}
else if (table.Columns[0].DataType == typeof(Int32))
{
queryBuilder.AppendFormat("{0}", table.Rows[0].Field<Int32?>(table.Columns[0].ColumnName) ?? 0);
}
else
{
queryBuilder.AppendFormat(", {0}", table.Rows[0][table.Columns[0].ColumnName].ToString());
}

for (int i = 1; i < table.Columns.Count; i++)
{
// escape String & Datetime values!
if (table.Columns[i].DataType == typeof(String))
{
queryBuilder.AppendFormat(", '{0}'", MySqlHelper.EscapeString(table.Rows[0][table.Columns[i].ColumnName].ToString()));
}
else if (table.Columns[i].DataType == typeof(DateTime))
{
dt = (DateTime)table.Rows[0][table.Columns[i].ColumnName];
queryBuilder.AppendFormat(", '{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));

}
else if (table.Columns[i].DataType == typeof(Int32))
{
queryBuilder.AppendFormat(", {0}", table.Rows[0].Field<Int32?>(table.Columns[i].ColumnName) ?? 0);
}
else
{
queryBuilder.AppendFormat(", {0}", table.Rows[0][table.Columns[i].ColumnName].ToString());
}
}

queryBuilder.Append(")");
queryBuilder.AppendLine();

// build all values all remaining rows
if (table.Rows.Count > 1)
{
// iterate over the rows
for (int row = 1; row < table.Rows.Count; row++)
{
// open value block
queryBuilder.Append(", (");

// escape String & Datetime values!
if (table.Columns[0].DataType == typeof(String))
{
queryBuilder.AppendFormat("'{0}'", MySqlHelper.EscapeString(table.Rows[row][table.Columns[0].ColumnName].ToString()));
}
else if (table.Columns[0].DataType == typeof(DateTime))
{
dt = (DateTime)table.Rows[row][table.Columns[0].ColumnName];
queryBuilder.AppendFormat("'{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));
}
else if (table.Columns[0].DataType == typeof(Int32))
{
queryBuilder.AppendFormat("{0}", table.Rows[row].Field<Int32?>(table.Columns[0].ColumnName) ?? 0);
}
else
{
queryBuilder.AppendFormat(", {0}", table.Rows[row][table.Columns[0].ColumnName].ToString());
}

for (int col = 1; col < table.Columns.Count; col++)
{
// escape String & Datetime values!
if (table.Columns[col].DataType == typeof(String))
{
queryBuilder.AppendFormat(", '{0}'", MySqlHelper.EscapeString(table.Rows[row][table.Columns[col].ColumnName].ToString()));
}
else if (table.Columns[col].DataType == typeof(DateTime))
{
dt = (DateTime)table.Rows[row][table.Columns[col].ColumnName];
queryBuilder.AppendFormat(", '{0}'", dt.ToString("yyyy-MM-dd HH:mm:ss"));
}
else if (table.Columns[col].DataType == typeof(Int32))
{
queryBuilder.AppendFormat(", {0}", table.Rows[row].Field<Int32?>(table.Columns[col].ColumnName) ?? 0);
}
else
{
queryBuilder.AppendFormat(", {0}", table.Rows[row][table.Columns[col].ColumnName].ToString());
}
} // end for (int i = 1; i < table.Columns.Count; i++)

// close value block
queryBuilder.Append(")");
queryBuilder.AppendLine();

} // end for (int r = 1; r < table.Rows.Count; r++)

// sql delimiter =)
queryBuilder.Append(";");

} // end if (table.Rows.Count > 1)

return queryBuilder.ToString();
}
else
{
return "";
} // end if(table.Columns.Count > 1 && table.Rows.Count > 0)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

关于c# - 写一个大数据表到mysql数据库c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17719181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com