2011-09-20 81 views
0

我一直在試圖找出在C#.net中編碼和使用OleDbCommand時UPDATE語句的正確語法。我覺得我所擁有的是正確的,但肯定不是因爲我仍然收到錯誤:「UPDATE語句中的語法錯誤」。我的代碼在我的更新按鈕下面發佈。我已經檢查了許多站點(包括Stackoverflow),以查看創建SQL語句的正確語法和標準,但仍未找到不會給我一個語法錯誤的語法和標準。任何的意見都將會有幫助。謝謝!UPDATE語句中的語法錯誤Visual Studio C#.net

private void update_button_Click(object sender, EventArgs e) 
    { 
     String ssize = ""; 
     foreach (Control control in Controls) 
     { 
      if (control != null && control is RadioButton) 
      { 
       RadioButton radio = control as RadioButton; 
       if (radio.Checked == true) 
       { 
        ssize = control.Text; 
       } 
      } 
     } 
     OleDbConnection myCon = new OleDbConnection(connString); 
     OleDbCommand cmd = new OleDbCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "UPDATE Youth SET FName = ?, MI = ?, LName = ?, Street = ?, CityStateZip = ?, PG = ?, HomePN = ?, WorkPN = ?, CellPN = ?, YEmail = ?, PGEmail = ?, Age = ?, DOB = ?, HS = ?, Grade = ?, PrevExp = ?, SSize = ?, Allergies = ?, MedConditions = ?, Avail = ?, FirstDep = ?, SecDep = ?, ThirdDep = ?, Fundraiser_1 = ?, Fundraiser_2 = ?, Fundraiser_3 = ?, Fundraiser_4 = ?, WHERE ID = ?"; 
     cmd.Parameters.Add("@FName", OleDbType.VarChar).Value = fname_tb.Text; 
     cmd.Parameters.Add("@MI", OleDbType.VarChar).Value = mi_tb.Text; 
     cmd.Parameters.Add("@LName", OleDbType.VarChar).Value = lname_tb.Text; 
     cmd.Parameters.Add("@Street", OleDbType.VarChar).Value = address1_tb.Text; 
     cmd.Parameters.Add("@CityStateZip", OleDbType.VarChar).Value = address2_tb.Text; 
     cmd.Parameters.Add("@PG", OleDbType.VarChar).Value = pg_tb.Text; 
     cmd.Parameters.Add("@HomePN", OleDbType.VarChar).Value = homePN_tb.Text; 
     cmd.Parameters.Add("@WorkPN", OleDbType.VarChar).Value = workPN_tb.Text; 
     cmd.Parameters.Add("@CellPN", OleDbType.VarChar).Value = cellPN_tb.Text; 
     cmd.Parameters.Add("@YEmail", OleDbType.VarChar).Value = yemail_tb.Text; 
     cmd.Parameters.Add("@PGEmail", OleDbType.VarChar).Value = pgemail_tb.Text; 
     cmd.Parameters.Add("@Age", OleDbType.VarChar).Value = age_tb.Text; 
     cmd.Parameters.Add("@DOB", OleDbType.Date).Value = bd_picker.Text; 
     cmd.Parameters.Add("@HS", OleDbType.VarChar).Value = hs_tb.Text; 
     cmd.Parameters.Add("@Grade", OleDbType.Integer).Value = grade_tb.Text; 
     cmd.Parameters.Add("@PrevExp", OleDbType.Integer).Value = prevexp_tb.Text; 
     cmd.Parameters.Add("@SSize", OleDbType.VarChar).Value = ssize; 
     cmd.Parameters.Add("@Allergies", OleDbType.VarChar).Value = food_tb.Text; 
     cmd.Parameters.Add("@MedConditions", OleDbType.VarChar).Value = special_tb.Text; 
     cmd.Parameters.Add("@Avail", OleDbType.VarChar).Value = week1_tb.Text + "," + week2_tb.Text; 
     cmd.Parameters.Add("@FirstDep", OleDbType.Date).Value = deposit_picker.Text; 
     cmd.Parameters.Add("@SecDep", OleDbType.Date).Value = second_picker.Text; 
     cmd.Parameters.Add("@ThirdDep", OleDbType.Date).Value = third_picker.Text; 
     cmd.Parameters.Add("@Fundraiser_1", OleDbType.Date).Value = fund1Picker.Text; 
     cmd.Parameters.Add("@Fundraiser_2", OleDbType.Date).Value = fund2Picker.Text; 
     cmd.Parameters.Add("@Fundraiser_3", OleDbType.Date).Value = fund3Picker.Text; 
     cmd.Parameters.Add("@Fundraiser_4", OleDbType.Date).Value = fund4Picker.Text; 
     cmd.Parameters.Add("@ID", OleDbType.Integer).Value = idTB.Text; 

     cmd.Connection = myCon; 
     try 
     { 
      myCon.Open(); 
      cmd.ExecuteNonQuery(); 
      myCon.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Problem opening connection!\n" + ex.Message, "Exception"); 
     } 
    } 
+0

[SSCCE後](http://sscce.org/)。具體而言,請嘗試將代碼示例降低到更可管理的大小。這樣做很可能會讓你自己找到答案。 –

+0

只是一個建議 - 當調試語法錯誤時,在一個較小的集合上工作,更新1個字段而不是所有的字段。這樣更容易。 – Beth

+0

@Michael:你如何在不改變代碼含義的情況下使它更小? –

回答

4

您在SQL中的WHERE子句之前有一個逗號。

cmd.CommandText = "UPDATE Youth SET FName = ?, MI = ?, LName = ?, Street = ?, 
    CityStateZip = ?, PG = ?, HomePN = ?, WorkPN = ?, CellPN = ?, YEmail = ?, 
    PGEmail = ?, Age = ?, DOB = ?, HS = ?, Grade = ?, PrevExp = ?, SSize = ?, 
    Allergies = ?, MedConditions = ?, Avail = ?, FirstDep = ?, SecDep = ?, 
    ThirdDep = ?, Fundraiser_1 = ?, Fundraiser_2 = ?, Fundraiser_3 = ?, 
    Fundraiser_4 = ?, <<--- extra comma 
    WHERE ID = ?"; 
+0

哇!謝謝!!最有可能的複製粘貼類型的錯誤,因爲SQL語句太長,但再次感謝! – Brandon

2

你的錯誤(或其中至少一個)就在這裏:

Fundraiser_4 = 哪裏..​​.

取出時,?