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;
}
どのDBMS?また、ここでは – ForguesR
... 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だと思います。 –
このdbアクションに 'ExecuteNonQuery'を使用しました。そして、私は 'Id 'の周りに大括弧を入れました。そして、あなたは 'Id'が書き込み可能である(つまり、アイデンティティ仕様ではない)のですか? –