C# の loop DataTable について質問があります
DataTable には約 20.000 行と 60 列があります
DataTable のすべてのデータをデータベースに挿入する SQL クエリを作成したい
My Sqlを使用しています
これは私のSQL構文です:
"Insert into a values (...), (...), (...), ..."
"(...)" は 1 行のすべてのデータ
これは C# コードです:
DataTable myDataTable; // myDataTable has 20.000 rows and 60 columns
string mySql = "insert into a values "
for(int iRow = 0; iRow < myDataTable.Rows.Count; iRow++)
{
mySql += "(";
for(int iColumn = 0; iColumn < myDataTable.Columns.Count; iColumn++)
{
string value = myDataTable.Rows[iRow][iColumn].ToString()
string parameter = "@" + iRows.ToString() + iColumns.ToString();
// here is some code for add "parameter" and "value" to MySqlCommand
mySql += ","
}
// remove "," at the end of string mySql
mySql = mySql.SubString(0, mySql.Length - 1);
mySql += "),"
}
// remove "," at the end of string mySql
// after that, I will execute mySql command to insert data into database here
そのコードを実行すると、完了するまでに時間がかかります
「string」から「StringBuilder」に変更してみましたが、少しだけ速くなりました。
コードをより速く実行するにはどうすればよいですか?
すべてのサポートに感謝します。