在我的.NET Windows應用程序中,我需要每次插入大約10百萬個數據。但是,根據我的低效代碼,需要超過5個minuites才能保存到SQL Server中。是否有最佳方法可最大限度地節省存儲時間那些數據?如何提高在.NET窗口應用程序中保存多行的效率?
private void saveAllDataInGrid()
{
int rowCount = dataGridView1.RowCount;
String str = "server=DESKTOP-TDV8JQ7;database=ExcelFileApp;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(str);
con.Open();
for (int count = 0; count < rowCount; count++)
{
try
{
String query = "insert into TemMainTable values (@p1,@p2,@p3,@p4,@p5,@p6,@p7,@p8,@p9,@p10,@p11,@p12,@p13,@p14,@p15,@p16,@p17,@p18,@p19,@p20,@p21,@p22,@p23)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@p1", dataGridView1.Rows[count].Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@p2", dataGridView1.Rows[count].Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@p3", dataGridView1.Rows[count].Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@p4", dataGridView1.Rows[count].Cells[3].Value.ToString());
cmd.Parameters.AddWithValue("@p5", dataGridView1.Rows[count].Cells[4].Value.ToString());
cmd.Parameters.AddWithValue("@p6", dataGridView1.Rows[count].Cells[5].Value.ToString());
cmd.Parameters.AddWithValue("@p7", dataGridView1.Rows[count].Cells[6].Value.ToString());
cmd.Parameters.AddWithValue("@p8", dataGridView1.Rows[count].Cells[7].Value.ToString());
cmd.Parameters.AddWithValue("@p9", dataGridView1.Rows[count].Cells[8].Value.ToString());
cmd.Parameters.AddWithValue("@p10", dataGridView1.Rows[count].Cells[9].Value.ToString());
cmd.Parameters.AddWithValue("@p11", dataGridView1.Rows[count].Cells[10].Value.ToString());
cmd.Parameters.AddWithValue("@p12", dataGridView1.Rows[count].Cells[10].Value.ToString());
cmd.Parameters.AddWithValue("@p13", dataGridView1.Rows[count].Cells[12].Value.ToString());
cmd.Parameters.AddWithValue("@p14", dataGridView1.Rows[count].Cells[13].Value.ToString());
cmd.Parameters.AddWithValue("@p15", dataGridView1.Rows[count].Cells[14].Value.ToString());
cmd.Parameters.AddWithValue("@p16", dataGridView1.Rows[count].Cells[15].Value.ToString());
cmd.Parameters.AddWithValue("@p17", dataGridView1.Rows[count].Cells[16].Value.ToString());
cmd.Parameters.AddWithValue("@p18", dataGridView1.Rows[count].Cells[17].Value.ToString());
cmd.Parameters.AddWithValue("@p19", dataGridView1.Rows[count].Cells[18].Value.ToString());
cmd.Parameters.AddWithValue("@p20", dataGridView1.Rows[count].Cells[19].Value.ToString());
cmd.Parameters.AddWithValue("@p21", dataGridView1.Rows[count].Cells[20].Value.ToString());
cmd.Parameters.AddWithValue("@p22", dataGridView1.Rows[count].Cells[21].Value.ToString());
cmd.Parameters.AddWithValue("@p23", dataGridView1.Rows[count].Cells[22].Value.ToString());
int i = cmd.ExecuteNonQuery();
lblDataSaved.Text = "No of rows Saved : " + rowCount;
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
con.Close();
MessageBox.Show("Successfully Saved");
}
謝謝!!
1.做在一個事務中,2使用存儲過程,3並行化代碼 –
看:https://docs.microsoft.com/en-us/dotnet/framework/data/ adonet/sql /表值參數 –
它看起來像你正在傳遞一個數據庫視圖到一個SQL服務器數據庫。 datagridview的數據來自哪裏?你不能告訴我有人已經將一百萬行數據輸入到datagridview中。 –