2016-12-15 112 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