使用C#高效地在MySQL数据库表中插入数千行
我需要在MySQL数据库中插入未知数量的行。这些行将出现在字典的列表中。 列表中的每个词典代表一行。对于每一行,Dictionary使用C#高效地在MySQL数据库表中插入数千行,c#,mysql,C#,Mysql,我需要在MySQL数据库中插入未知数量的行。这些行将出现在字典的列表中。 列表中的每个词典代表一行。对于每一行,Dictionary键包含列名,值包含该行的列数据 我已经实现了这样的目标- public long InsertMultiple(string TableName, List<Dictionary<string, string>> listMultipleRows) { try { string columnNames = nu
键
包含列名,值
包含该行的列数据
我已经实现了这样的目标-
public long InsertMultiple(string TableName, List<Dictionary<string, string>> listMultipleRows)
{
try
{
string columnNames = null;
StringBuilder sCommand = new StringBuilder();
List<string> Rows = new List<string>();
int columnLength = listMultipleRows.First().Select(x => x.Key).ToArray().Length;
//Fetching required column names.
if (listMultipleRows.Count > 0)
columnNames = string.Join(", ", listMultipleRows.First().Select(x => x.Key).ToArray());
//Preparing command
if (columnNames != null)
sCommand = new StringBuilder("INSERT INTO " + TableName + " (" + columnNames + ") VALUES ");
//Preparing column format like - '{0}','{1}'......
string columnFormat = "(";
for (int i = 0; i < columnLength; i++)
{
if (i != 0)
columnFormat = columnFormat + ",";
columnFormat = columnFormat + "'{" + i + "}'";
}
columnFormat = columnFormat + ")";
//Appending each row values. Actual rows which needs to be inserted.
foreach (Dictionary<string, string> row in listMultipleRows)
{
Rows.Add(string.Format(columnFormat, row.Select(x => MySql.Data.MySqlClient.MySqlHelper.EscapeString(x.Value)).ToArray()));
}
sCommand.Append(string.Join(",", Rows));
sCommand.Append(";");
MySqlCommand Comm = new MySqlCommand();
Comm.CommandText = sCommand.ToString();
Comm.Connection = m_Conn;
//One shot insertion operation
return Convert.ToInt64(ExecuteScalar(Comm));
}
catch (Exception Ex)
{
return 0;
}
}
public long InsertMultiple(字符串表名,列表列表multiplerows)
{
尝试
{
字符串columnNames=null;
StringBuilder sCommand=新的StringBuilder();
列表行=新列表();
int columnLength=listMultipleRows.First().Select(x=>x.Key).ToArray().Length;
//获取所需的列名。
如果(listMultipleRows.Count>0)
columnNames=string.Join(“,”,listMultipleRows.First().Select(x=>x.Key.ToArray());
//准备命令
if(columnNames!=null)
sCommand=新的StringBuilder(“插入到“+TableName+”(“+columnNames+”)值中”);
//正在准备列格式,如-'{0}','{1}'。。。。。。
string columnFormat=“(”;
for(int i=0;iMySql.Data.MySqlClient.MySqlHelper.EscapeString(x.Value)).ToArray());
}
追加(string.Join(“,”行));
sCommand.Append(“;”);
MySqlCommand Comm=新的MySqlCommand();
Comm.CommandText=sCommand.ToString();
通信连接=m_连接;
//一次性插入操作
返回Convert.ToInt64(ExecuteScalar(Comm));
}
捕获(例外情况除外)
{
返回0;
}
}
2800条记录的执行时间约为500毫秒,这是极好的。
我在这里担心的是SQL注入。通常,我们使用
参数化查询以避免SQL注入。但在我的情况下,行
数量可能很大,因此无法创建这么多参数。有
这个问题的解决方案?还是更好的办法
我看不出循环输入与串联和绑定变量之间的区别。特别是如果你使用PDO和as来注射,这取决于你从哪里得到你的列表!这是用户输入吗?@riggsfully它是通过反序列化某个JSON文件准备的字典。在某种程度上,是的,这是一个用户输入。@RiggsFolly参数在特定情况下可能超过30k。正如我所知,有2100的限制。如果你真的想批量插入一个大文件,你可以转储/转换列表到一个临时的分隔文件,并使用MySqlBulkLoader。我看不到循环输入和连接绑定变量之间的区别。特别是如果你使用PDO和as来注射,这取决于你从哪里得到你的列表!这是用户输入吗?@riggsfully它是通过反序列化某个JSON文件准备的字典。在某种程度上,是的,这是一个用户输入。@RiggsFolly参数在特定情况下可能超过30k。据我所知,有2100的限制。如果您真的想批量插入一个大文件,您可以将列表转储/转换为一个临时分隔文件,并使用MySqlBulkLoader。