日期:2014-05-18  浏览次数:20842 次

关于C#中,数据库数据的删除操作
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语句出错,求解

------解决方案--------------------
"delete " + table + " where (empID = '" + empId + "') ";

去掉table的引号,如果empID 字段是数值型,就不要加引号
------解决方案--------------------
改成这样
cmd.CommandText = "delete " + table + " where empID = '" + empId + "' ";


------解决方案--------------------
应该是:
"delete table where empID='"+empId+"'";