我試圖從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;
}
哪個DBMS?另外需要tbl_Gegevens'的'表定義... – ForguesR
這裏是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。 –
我會爲這個數據庫操作使用'ExecuteNonQuery'。我會在方括號裏面加上'Id'。你確定'Id'是可寫的(即不是身份規範)? –