2016-12-15 15 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] funtie[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?また、ここでは – ForguesR

+0

... tbl_Gegevens' 'のテーブル定義を必要とするtbl_Gegevensの仕様です:。 はTABLE [dboは] [tbl_Gegevens]( [ID]は、NOT NULL INT、 は[voornaam] NVARCHAR(50)NULLをCREATE 、 [achternaam] NVARCHAR(50)NULL、 [geboortedatum] DATEのNULL、 [functie] NVARCHAR(50)NULL、 [uurloon] SMALLMONEYのNULL、 PRIMARY KEY CLUSTERED([ID] ASC) )。 私のdbmsはADO.Netだと思います。 –

+2

このdbアクションに 'ExecuteNonQuery'を使用しました。そして、私は 'Id 'の周りに大括弧を入れました。そして、あなたは 'Id'が書き込み可能である(つまり、アイデンティティ仕様ではない)のですか? –

答えて

4

あなたのコードのUPDATEの一部で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

関連する問題