2016-12-15 173 views
0

我試圖從Winforms應用程序向SQL Server表中插入一個新行。據我所知,我的查詢是正確的,但Visual Studio中保持返回一個錯誤:'achternaam'附近的語法不正確

Incorrect syntax near 'achternaam'

我希望有人能指出我在正確的方向。

public void UpdateGegevens(int id, string voornaam, string achternaam, string functie, DateTime geboortedatum, decimal uurloon) 
{ 
if (ReturnFirstTime(id) == true) 
{ 
    using (SqlConnection con = new SqlConnection(connectionString)) 
    { 
     using (SqlCommand command = new SqlCommand()) 
     { 
      command.Connection = con; 
      command.CommandType = CommandType.Text; 
      command.CommandText = "INSERT INTO tbl_Gegevens (Id, voornaam, achternaam, geboortedatum, functie, uurloon) VALUES (@Id, @vn, @an, @gb, @f, @ul);"; 

      command.Parameters.Add("@Id", SqlDbType.Int).Value = id; 
      command.Parameters.Add("@vn", SqlDbType.VarChar).Value = voornaam; 
      command.Parameters.Add("@an", SqlDbType.VarChar).Value = achternaam; 
      command.Parameters.Add("@f", SqlDbType.VarChar).Value = functie; 
      command.Parameters.Add("@gb", SqlDbType.Date).Value = geboortedatum; 
      command.Parameters.Add("@ul", SqlDbType.Money).Value = uurloon; 

      try 
      { 
       con.Open(); 
       command.ExecuteScalar(); 
      } 
      catch (SqlException ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 
      finally 
      { 
       con.Close(); 
      } 
     } 
    } 
} 
     else 
     { 
      using (SqlConnection con = new SqlConnection(connectionString)) 
      { 
       using (SqlCommand command = new SqlCommand()) 
       { 
        command.Connection = con; 
        command.CommandType = CommandType.Text; 
        command.CommandText = "UPDATE tbl_Gegevens SET [email protected] [email protected] [email protected] [email protected] [email protected] WHERE Id = @Id;"; 
        command.Parameters.AddWithValue("@Id", id); 
        command.Parameters.AddWithValue("@vn", voornaam); 
        command.Parameters.AddWithValue("@an", achternaam); 
        command.Parameters.AddWithValue("@gb", geboortedatum); 
        command.Parameters.AddWithValue("@f", functie); 
        command.Parameters.AddWithValue("@ul", uurloon); 
        try 
        { 
         con.Open(); 
         command.ExecuteNonQuery(); 
        } 
        catch (SqlException ex) 
        { 
         System.Windows.Forms.MessageBox.Show(ex.Message); 
        } 
        finally 
        { 
         con.Close(); 
        } 
       } 
      } 
     } 
    } 

這裏是tbl_Gegevens規格:

create table [dbo].[tbl_Gegevens] (
    [Id] int not null 
, [voornaam] nvarchar(50) null 
, [achternaam] nvarchar(50) null 
, [geboortedatum] date null 
, [functie] nvarchar(50) null 
, [uurloon] smallmoney null 
, primary key clustered ([Id] asc) 
); 

我覺得我的DBMS是ADO.Net。

這是我傳遞的信息的方法的方式:

private void btnConfirm_Click(object sender, EventArgs e) 
    { 
     if (tbName.Text != "" && tbSurname.Text != "" && tbFunction.Text 
      != "" && dtpBirthdate.Value != date && nudSalary.Value != 0) 
      { 
       Database1.SetFirstTime(ID); 
       Database1.UpdateGegevens(ID, tbName.Text, tbSurname.Text, tbFunction.Text, dtpBirthdate.Value, nudSalary.Value); 
       this.Hide(); 
       frmMain fm = new frmMain(ID); 
       fm.Show(); 
      } 
      else 
      { 
       MessageBox.Show("Vul alle velden in!"); 
      } 
     } 

這是我用它來得到我的ID查詢:

public int ReturnLoginID(string username, string password) 
    { 
     SqlConnection con = new SqlConnection(connectionString); 
     SqlCommand cmd = new SqlCommand("Select * from tbl_Login where [email protected] and [email protected]", con); 
     cmd.Parameters.AddWithValue("@username", username); 
     cmd.Parameters.AddWithValue("@password", password); 
     int ID = 9999; 
     con.Open(); 
     using (SqlDataReader reader = cmd.ExecuteReader()) 
     { 
      reader.Read(); 
      ID = reader.GetInt32(0); 
     } 
     con.Close(); 
     return ID; 
    } 
+1

哪個DBMS?另外需要tbl_Gegevens'的'表定義... – ForguesR

+0

這裏是tbl_Gegevens的規範: CREATE TABLE [DBO] [tbl_Gegevens]( [ID] INT NOT NULL, [voornaam] NVARCHAR(50)NULL , [achternaam] NVARCHAR(50)NULL, [geboortedatum] DATE NULL, [functie] NVARCHAR(50)NULL, [uurloon] SMALLMONEY NULL, PRIMARY KEY CLUSTERED([ID] ASC) ); 我想我的dbms是ADO.Net。 –

+2

我會爲這個數據庫操作使用'ExecuteNonQuery'。我會在方括號裏面加上'Id'。你確定'Id'是可寫的(即不是身份規範)? –

回答

4

在你的代碼的更新零件有沒有逗號分隔SET列表中的字段

command.CommandText = @"UPDATE tbl_Gegevens SET [email protected], 
         [email protected], [email protected], 
         [email protected], [email protected] WHERE Id = @Id;"; 

我認爲這個問題可以用來強調使用調試器的重要性。如果您使用調試程序遍歷代碼,則可以更快地解決此問題。

+0

不錯的發現! (*代碼中帶有整個'ReturnFirstTime(id)== true')* – Igor

+0

這種類型的戲劇可能已經被[使用存儲過程]阻止了(http://stackoverflow.com/a/7542564/6936343)! –

+2

@Tony_KiloPapaMikeGolf存儲過程並不是萬能的靈丹妙藥。如果你沒有戴上DBA帽子,那麼使用它們來完成這些簡單的任務會導致維護噩夢。 – Steve