任何人都可以幫助提高性能嗎?更新表需要很多時間。
我正在將 datagridview 中的序列號更新為一個名為 dbo.json 的表
// UPDATE dbo.json with numbers
private void BtnUpdateSql_Click(object sender, EventArgs e)
{
string VAL1;
string VAL2;
foreach (DataGridViewRow row in DgvWhistlSorted.Rows)
if (string.IsNullOrEmpty(row.Cells[5].Value as string))
{
}
else
{
for (int i = 0; i <= DgvWhistlSorted.Rows.Count - 2; i )
{
VAL1 = DgvWhistlSorted.Rows[i].Cells[6].Value.ToString();
VAL2 = DgvWhistlSorted.Rows[i].Cells[0].Value.ToString();
var cnn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
using (var con = new SqlConnection(cnn))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE dbo.json SET RowN = @VAL1 WHERE [A-order] = @VAL2";
cmd.Parameters.AddWithValue("@VAL1", VAL1);
cmd.Parameters.AddWithValue("@VAL2", VAL2);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
MessageBox.Show("dbo.json is ready");
}
uj5u.com熱心網友回復:
您不應該在如此緊密的回圈中創建連接和命令 - 在回圈之前創建并打開連接和命令ONCE,并且在回圈中,只設定引數值并為每個條目執行查詢。
像這樣的東西:
// UPDATE dbo.json with numbers
private void BtnUpdateSql_Click(object sender, EventArgs e)
{
string VAL1;
string VAL2;
// define connection string, query text *ONCE* before the loop
string cnn = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
string updateQuery = "UPDATE dbo.json SET RowN = @VAL1 WHERE [A-order] = @VAL2;";
// create connection and command *ONCE*
using (SqlConnection con = new SqlConnection(cnn))
using (SqlCommand cmd = new SqlCommand(updateQuery, cnn))
{
// Define parameters - adapt as needed (don't know the actual datatype they have)
cmd.Parameters.Add("@VAL1", SqlDbType.VarChar, 100);
cmd.Parameters.Add("@VAL2", SqlDbType.VarChar, 100);
// open connection ONCE, for all updates
con.Open();
foreach (DataGridViewRow row in DgvWhistlSorted.Rows)
{
if (!string.IsNullOrEmpty(row.Cells[5].Value as string))
{
for (int i = 0; i <= DgvWhistlSorted.Rows.Count - 2; i )
{
VAL1 = DgvWhistlSorted.Rows[i].Cells[6].Value.ToString();
VAL2 = DgvWhistlSorted.Rows[i].Cells[0].Value.ToString();
// set the values
cmd.Parameters["@VAL1"].Value = VAL1;
cmd.Parameters["@VAL2"].Value = VAL2;
// execute query
cmd.ExecuteNonQuery();
}
}
}
// close connection after all updates are done
con.Close();
}
MessageBox.Show("dbo.json is ready");
}
uj5u.com熱心網友回復:
一次創建連接...您每次通過回圈創建一個新的資料庫連接!事實上,您不需要每次都創建新的命令物件。您可以重復使用命令物件,因為引數相同。每次通過回圈清除引數即可。
也不要在回圈中進行網格視圖計數,為其設定一個變數。
string query = "UPDATE dbo.json SET RowN = @VAL1 WHERE [A-order] = @VAL2";
int counter = DgvWhistlSorted.Rows.Count - 2;
using (SqlConnection con = new SqlConnection(cnn))
{
con.Open();
using(SqlCommand cmd = new SqlCommand(cnn,query))
{
cmd.Parameters.Clear();
//Do your loop in here
for (int i = 0; i <= counter; i )
{
VAL1 = DgvWhistlSorted.Rows[i].Cells[6].Value.ToString();
VAL2 = DgvWhistlSorted.Rows[i].Cells[0].Value.ToString();
cmd.Parameters.AddWithValue("@VAL1", VAL1);
cmd.Parameters.AddWithValue("@VAL2", VAL2);
cmd.ExecuteNonQuery();
}
}
}
uj5u.com熱心網友回復:
一個更好的主意是在一個命令中執行此操作,通過在表值引數 (TVP)中傳遞所有資料:
首先創建表型別。我不知道你的資料型別,所以我在這里猜測。確保將型別與現有表匹配。
CREATE TYPE dbo.OrderJson (
Order int PRIMARY KEY,
RowN nvarchar(max) NOT NULL
);
然后,您可以一批通過整個事情。您需要創建一個DataTable作為引數傳遞,或者您可以使用現有的資料表。
// UPDATE dbo.json with numbers
private void BtnUpdateSql_Click(object sender, EventArgs e)
{
var table = new DataTable {
Columns = {
{ "Order", typeof(int) },
{ "RowN", typeof(string) },
},
};
foreach (DataGridViewRow row in DgvWhistlSorted.Rows)
if (!string.IsNullOrEmpty(row.Cells[5].Value as string))
table.Rows.Add(DgvWhistlSorted.Rows[i].Cells[0].Value, DgvWhistlSorted.Rows[i].Cells[6].Value)
const string query = @"
UPDATE dbo.json
SET RowN = t.RowN
FROM dbo.json j
JOIN @tmp t ON t.order = j.[A-order];
";
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString))
using (var cmd = new SqlCommand(query, con))
{
cmd.Parameters.Add(new SqlParameter("@tmp", SqlDbType.Structured) { Value = table, TypeName = "dbo.OrderJson" });
con.Open();
cmd.ExecuteNonQuery();
}
MessageBox.Show("dbo.json is ready");
}
uj5u.com熱心網友回復:
我發現最快的方法是將 DATAGRIDVIEW 保存到 SQL 表并繼續該程序 - 存盤程序 更新查詢 - 在兩個表之間 - 現在它飛了......謝謝大家
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/459255.html
