请问C#数据库删除怎么谢啊
string deleCmd="DELETE FROM student WHERE id='"+textBox1.Text +"'";
SqlCommand myCommand=new SqlCommand (deleCmd,conn);myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
提示有错误啊

解决方案 »

  1.   

    string deleCmd="DELETE FROM student WHERE id=" + textBox1.Text;
      

  2.   

    string insertCmd="INSERT INTO student(id,name)VALUES(@id,@name)";SqlCommand myCommand=new SqlCommand (insertCmd,conn);
    myCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int ,4) );
    myCommand.Parameters["@id"].Value =int.Parse(textBox1.Text) ;
    myCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.Int ,4) );
    myCommand.Parameters["@name"].Value =int.Parse(textBox2.Text) ;myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    这是我的添加数据,是正确的,就是不知道删除
      

  3.   

    如果id是int类型的那么两边不需要单引号
      

  4.   

    如果id是int类型的那么两边不需要单引号
      

  5.   

    string insertCmd="INSERT INTO student(id,name)VALUES(@id,@name)";
    string deleCmd="DELETE FROM student WHERE id=@id";SqlCommand myCommand=new SqlCommand (deleCmd,conn);
    myCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int ,4) );
    myCommand.Parameters["@id"].Value =int.Parse(textBox1.Text) ;myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
      

  6.   

    string deleCmd="DELETE FROM student WHERE id=@id";SqlCommand myCommand=new SqlCommand (deleCmd,conn);
    myCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int ,4) );
    myCommand.Parameters["@id"].Value =int.Parse(textBox1.Text) ;myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
      

  7.   

    id=@id也试提示myCommand.ExecuteNonQuery();错误哦
    连接数据库没错啊,我的添加都是成功的
      

  8.   

    string deleCmd="DELETE * FROM student WHERE id='"+textBox1.Text +"'";试试
      

  9.   

    private void button5_Click_1(object sender, System.EventArgs e)
    {
    if ((this.BindingContext[ds,"student"].Count > 0)) 
    {
    this.BindingContext[ds,"student"].RemoveAt(this.BindingContext[ds,"student"].Position);
        
    }
    }
      

  10.   

    id是int类型的,不需要单引号。
    不是这的问题的话,就是约束的了,检查一下关系图
      

  11.   

    在查询分析器里先检查你的sql语句??
      

  12.   

    1.如果你想删除的是datagrid表中的一行:private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) 

    foreach(DataGridItem di in this.DataGrid1.Items) 

    if(di.ItemType==ListItemType.Item||di.ItemType==ListItemType.AlternatingItem||di.ItemType==ListItemType.EditItem) 

    ((LinkButton)di.Cells[0].Controls[0]).Attributes.Add("onclick","return confirm('您确实要删除这行记录吗?');");


    }private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) 

    String Con = ConfigurationSettings.AppSettings["con"];
    SqlConnection con = new SqlConnection(Con);
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("select * from " + Table, con);
    SqlCommandBuilder cb=new SqlCommandBuilder(da);
    DataSet ds=new DataSet();
    da.Fill(ds,"table1");
    DataTable table=ds.Tables["table1"];
    table.PrimaryKey=new DataColumn[]{table.Columns["id"]};
    if(e.CommandName == "del")
    {
    con.Open();
    SqlCommand cmd = new SqlCommand("delete from "+Table+" where id=@id", con);   
    SqlParameter parm1=new SqlParameter("@id",SqlDbType.Int); 
    parm1.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex]; 
    cmd.Parameters.Add(parm1); 
    cmd.ExecuteNonQuery();
    con.Close();
    }
    }
    2.如果就按你的代码看的话,那好象应该是textBox1.Text的问题,去掉首尾空格.
     textBox1.Text.Trim()
    因为输入的内容后面包含空格,在数据库表里就查不到这个id了.