using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WindowsApplication1
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public string table = "";
        private void btnDel_Click(object sender, EventArgs e)
        {
            table = "TB_EMP";
            string empId = textID.Text;
            string empName = textName.Text;
            string empSal = textSal.Text;
            string empSex = textSex.Text;
            string empBirthday = textBirthday.Text;
            string str = "Server= LYP19900208;DataBase=test01;Uid=sa;Pwd=6020";
            SqlConnection conn = null;
            conn = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            cmd.CommandText = "delete '" + table + "' where (empID = '" + empId + "') ";
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds,table);
            MessageBox.Show("删除成功!");
        }
    }
}
这是我的代码,运行的时候总是提示我sql语句出错,求解

解决方案 »

  1.   

    "delete " + table + " where (empID = '" + empId + "') ";去掉table的引号,如果empID 字段是数值型,就不要加引号
      

  2.   

    改成这样
    cmd.CommandText = "delete " + table + " where empID = '" + empId + "' ";
      

  3.   


    sql语句 删除是这样写的
    delete from table where tableid='';
    table是不需要加引号的
      

  4.   

    应该是:
    "delete table where empID='"+empId+"'";
     
      

  5.   

    cmd.CommandText = "delete '" + table + "' where (empID = '" + empId + "') ";改成cmd.CommandText = "delete from " + table + " where (empID = '" + empId + "') ";
      

  6.   

    "delete  from " + table + " where (empID = '" + empId + "') ";
    //删除table中的符合条件的记录"drop "+table
    //删除table