请问C#数据库删除怎么谢啊
string deleCmd="DELETE FROM student WHERE id='"+textBox1.Text +"'";
SqlCommand myCommand=new SqlCommand (deleCmd,conn);myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
提示有错误啊
string deleCmd="DELETE FROM student WHERE id='"+textBox1.Text +"'";
SqlCommand myCommand=new SqlCommand (deleCmd,conn);myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
提示有错误啊
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();
这是我的添加数据,是正确的,就是不知道删除
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();
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();
连接数据库没错啊,我的添加都是成功的
{
if ((this.BindingContext[ds,"student"].Count > 0))
{
this.BindingContext[ds,"student"].RemoveAt(this.BindingContext[ds,"student"].Position);
}
}
不是这的问题的话,就是约束的了,检查一下关系图
{
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了.