試試這個。代碼太多了,所以這里可能還有一兩個輸入錯誤或其他問題。請注意以下評論: private void InsertAll(){ DialogResult Result = MessageBox.Show("Do you want to save all ? ", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (Result != DialogResult.OK) return; //Guard clause to reduce indentation // Even better if you check this first, outside the method. using (var cn = new SqlConnection(Cn.ConnectionString)) // Using block around the connection using (var cmd = new SqlCommand("", cn)) { //removed the try/catch. Instead, wrap the try/catch around this method call cmd.CommandText = @" INSERT INTO DocDtls ( DocNum, z