簡體   English   中英

SQL語法錯誤-''附近的語法不正確

[英]SQL syntax error - incorrect syntax near ' '

我正在使用Visual Studio 2012,並創建了一個簡單的應用程序。 我創建了一個名為Personnel的數據庫,其中包含一個名為Employee的表。 我已經解決了所有語法錯誤,並且代碼看起來不錯,但是當我嘗試使用表單將數據插入數據庫時​​,Form1.cs引發了錯誤。

在我將數據插入ID,姓名,職位和小時工資率字段后,然后單擊添加后,它在Position字段中阻塞,錯誤讀取了我在該字段中鍵入的內容。

例如,如果我在該表單字段中輸入“ Manager”,則錯誤窗口顯示為:

'Manager'附近的語法不正確。

在這里的任何幫助將不勝感激。 這是我的代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Personnel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'personnelDataSet.Employee' table. You can move, or remove it, as needed.
            this.employeeTableAdapter.Fill(this.personnelDataSet.Employee);

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(global::Personnel.Properties.Settings.Default.PersonnelConnectionString);
            try
            {
                string sql = "INSERT INTO Employee (employeeID,Name,Position,HourlyPayRate) values("+txtemployeeID.Text+",'"+txtName.Text+",'"+txtPosition.Text+",'"+txtHourlyPayRate.Text+"')";
                SqlCommand exeSQL = new SqlCommand(sql, cn);
                cn.Open();
                exeSQL.ExecuteNonQuery();

                MessageBox.Show("New record added!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.employeeTableAdapter.Fill(this.personnelDataSet.Employee);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                cn.Close();
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            this.employeeTableAdapter.Fill(this.personnelDataSet.Employee);
        }
    }
}

請閱讀您帖子下的評論,他們回答了您的問題。

  1. 您的sql語法在其中一個字段值之后缺少單引號; 姓名– Andrew Barber
  2. SQL注入警報 -您不應將SQL語句連接在一起-可以使用參數化查詢來避免SQL注入– marc_s

調整代碼的方法如下:

string sql = @"INSERT INTO Employee (employeeID,Name,Position,HourlyPayRate) 
              VALUES(@employeeID, @Name, @Position, @HourlyPayRate )";
SqlCommand exeSQL = new SqlCommand(sql, cn);
cn.Open();
cmd.Parameters.AddWithValue("@employeeID", txtemployeeID.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Position", txtPosition.Text);
cmd.Parameters.AddWithValue("@HourlyPayRate", txtHourlyPayRate.Text);
exeSQL.ExecuteNonQuery();

閱讀SqlCommand.Parameters屬性

編輯:

AddWithValue 不是100%安全的 ,最好使用Add()進行顯式聲明,例如:

cmd.Parameters.Add("@employeeID", SqlDbType.Int).Value = Convert.ToInt32(txtemployeeID.Text);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
cmd.Parameters.Add("@Position", SqlDbType.VarChar).Value = txtPosition.Text;
cmd.Parameters.Add("@HourlyPayRate", SqlDbType.Decimal).Value = Convert.ToDecimal(txtHourlyPayRate.Text);

使用適當的SqlDbType枚舉並相應地轉換值。

試試這個

單個代碼應在每個文本框值之前和之后添加

 string sql = "INSERT INTO Employee (employeeID,Name,Position,HourlyPayRate) values('"+txtemployeeID.Text+"','"+txtName.Text+"','"+txtPosition.Text+"','"+txtHourlyPayRate.Text+"')";

似乎您應該添加轉義字符,因為values(“ + txtemployeeID.Text +” ...會在values(Manager,...中擴展,而不是values(“ Manager”,...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM