我必須找到作爲標識插入列「ID_K」表KLIENT中的最後一個數字。我應該如何改進我的代碼?用於查找IDENT_CURRENT的Sql命令
非常感謝。
SqlCommand comm = new SqlCommand("IDENT_CURRENT klient", spojeni);
// SqlCommand comm = new SqlCommand("Select MAX (ID_K) FROM klient", spojeni);
spojeni.Open();
int max = (int)comm.ExecuteScalar();
spojeni.Close();
foreach (DataGridViewRow row in dtg_ksluzby.Rows)
{
if (Convert.ToBoolean(row.Cells[3].Value) == true)
{
SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni);
prikaz2.Parameters.AddWithValue("@val1", row.Cells["text"].Value);
prikaz2.Parameters.AddWithValue("@val2", row.Cells["pocet"].Value);
prikaz2.Parameters.AddWithValue("@val3", row.Cells["akce"].Value);
prikaz2.Parameters.AddWithValue("@val4", max + 1);
spojeni.Open();
prikaz2.ExecuteNonQuery();
spojeni.Close();
}
}
'SELECT IDENT_CURRENT('tableName')'是正確的語法 –