我下面这段代码只能删除界面上的数据
但是就是删不掉数据库中的数据
请问各位高人,我还差些什么代码···
别见笑哈,我一个学土木的搞这个都还没有入门
痛苦啊···
private void button5_Click_1(object sender, EventArgs e)
{
if(MessageBox.Show(this, "确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlConnection conn = new SqlConnection("Server=PC-201106241704;Integrated Security=True;" + "DataBase=chengdu");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Connection.Open();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
try
{
if (dataGridView2.Rows[i].Cells[0].Value != null)
{
if (bool.Parse(dataGridView2.Rows[i].Cells[0].Value.ToString()) == true)
{
dataGridView2.Rows.RemoveAt(i);
i--;
}
}
}
catch { }
}
comm.Connection.Close();
MessageBox.Show("数据删除成功!", "提示");
RefreshList();
}
//Conn.Close();
}
但是就是删不掉数据库中的数据
请问各位高人,我还差些什么代码···
别见笑哈,我一个学土木的搞这个都还没有入门
痛苦啊···
private void button5_Click_1(object sender, EventArgs e)
{
if(MessageBox.Show(this, "确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlConnection conn = new SqlConnection("Server=PC-201106241704;Integrated Security=True;" + "DataBase=chengdu");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Connection.Open();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
try
{
if (dataGridView2.Rows[i].Cells[0].Value != null)
{
if (bool.Parse(dataGridView2.Rows[i].Cells[0].Value.ToString()) == true)
{
dataGridView2.Rows.RemoveAt(i);
i--;
}
}
}
catch { }
}
comm.Connection.Close();
MessageBox.Show("数据删除成功!", "提示");
RefreshList();
}
//Conn.Close();
}
--这个只是在你页面上删除了,执行不到数据库
要想在数据库中删除,得用delete 语句
{
if(MessageBox.Show(this, "确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlConnection conn = new SqlConnection("Server=PC-201106241704;Integrated Security=True;" + "DataBase=chengdu");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Connection.Open();
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
try
{
if (dataGridView2.Rows[i].Cells[0].Value != null)
{
if (bool.Parse(dataGridView2.Rows[i].Cells[0].Value.ToString()) == true)
{
dataGridView2.Rows.RemoveAt(i);
//delete数据,
//你没有和数据库交互啊
i--;
}
}
}
catch { }
}
comm.Connection.Close();
MessageBox.Show("数据删除成功!", "提示");
RefreshList();
}
//Conn.Close();
}
使用delete from语句是这个样么
delete from Table where name = 如果是这样的话
等于号的后面应该怎么写,等于号后面应该是checkbox选中的行
但是不知道该怎么去写···
//只是在页面控件中移除了一行
//要删除的话,需要
//delete from tablename where id=@id
你可以在程序里定义一个字符串sql,在去掉页面行的时候同时删除数据库的记录dataGridView2.Rows.RemoveAt(i);这里后边跟这个字符串sql, 具体是 delete from 表 where 主键列 = 你需要删除那条记录的主键值然后执行这个字符串。
private void button5_Click_1(object sender, EventArgs e)
{
if(MessageBox.Show(this, "确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlConnection conn = new SqlConnection("Server=PC-201106241704;Integrated Security=True;" + "DataBase=chengdu");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.Connection.Open();
string id="";
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
if (dataGridView2.Rows[i].Cells[0].Value != null)
{
if (bool.Parse(dataGridView2.Rows[i].Cells[0].Value.ToString()) == true)
{
id+= dataGridView2.Rows[i].Cells[1].Value.ToString()+“,”; //假设你第二列是主键ID
}
}
if(!string.IsNullOrEmpty(id))
{
string sql="delete from tb where id in ("+id.Trim(',')+")";
comm.CommandText=sql;
cmd.ExecuteNonQuery();
}
}
comm.Connection.Close();
MessageBox.Show("数据删除成功!", "提示");
RefreshList();
}
//Conn.Close();
}