2 calvinlp calvinlp 于 2015.05.26 00:26 提问

C#如何将DataGridView1中选中的行删除(SQLserver表中的主键为“学号”)

//删除按钮代码
private void dele_Click(object sender, EventArgs e)
{
string cnnstr = "Data Source=.;Database=stutable;User id=sa;PWD=m123";

        SqlConnection cnn = new SqlConnection(cnnstr);

        cnn.Open();
        int row=dataGridView1.SelectedRows.Count;
        if (row > 0)
        {
            for (int i = 0; i < row; i++)
            {
                string delstr = "DELETE from stu where 学号=DGVstr";
                string DGVstr = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                SqlCommand delecmd = new SqlCommand(delstr,cnn);
                delecmd.ExecuteNonQuery();

                MessageBox.Show("确认删除吗?","删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk);
            }

        }
        cnn.Close();
                    }

这段代码运行后没反应,DGV中数据行没删掉,数据库中也还在。请高人指点下,给看看吧。谢谢了!

3个回答

frank_20080215
frank_20080215   2015.05.26 06:06

string delstr = "DELETE from stu where 学号=DGVstr";
string DGVstr = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
改成这样:
string DGVstr = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
string delstr = "DELETE from stu where 学号=“ + "'" + DGVstr +"'";

                    SqlCommand delecmd = new SqlCommand(delstr,cnn);
        delecmd.ExecuteNonQuery();
qq_28486429
qq_28486429   2015.05.26 01:29

C#如何将DataGridView1中选中的行删除(SQLserver表中的主键为“学号”)
stringdatagridview
//删除按钮代码
private void dele_Click(object sender, EventArgs e)
{
string cnnstr = "Data Source=.;Database=stutable;User id=sa;PWD=m123";
SqlConnection cnn = new SqlConnection(cnnstr);

    cnn.Open();
    int row=dataGridView1.SelectedRows.Count;
    if (row > 0)
    {
        for (int i = 0; i < row; i++)
        {
qq_28486429
qq_28486429   2015.05.26 01:30

stringdatagridview
//删除按钮代码
private void dele_Click(object sender, EventArgs e)
{
string cnnstr = "Data Source=.;Database=stutable;User id=sa;PWD=m123";
SqlConnection cnn = new SqlConnection(cnnstr);

    cnn.Open();
    int row=dataGridView1.SelectedRows.Count;
    if (row > 0)
    {
        for (int i = 0; i < row; i++)
        {
            string delstr = "DELETE from stu where 学号=DGVstr";
            string DGVstr = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
            SqlCommand delecmd = new SqlCommand(delstr,cnn);
            delecmd.ExecuteNonQuery();

            MessageBox.Show("确认删除吗?","删除确认",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk);
        }

    }
    cnn.Close();
                }

这段代码运行后没反应,DGV中数据行没删掉,数据库中也还在。请高人指点下,给看看吧。谢谢了!
全部展开
举报||||
0
我要回答
1个回答
按赞数排序
qq_28486429 qq_28486429

C#如何将DataGridView1中选中的行删除(SQLserver表中的主键为“学号”)
stringdatagridview
//删除按钮代码
private void dele_Click(object sender, EventArgs e)
{
string cnnstr = "Data Source=.;Database=stutable;User id=sa;PWD=m123";
SqlConnection cnn = new SqlConnection(cnnstr);

cnn.Open();
int row=dataGridView1.SelectedRows.Count;
if (row > 0)
{
    for (int i = 0; i < row; i++)
    {
Csdn user default icon
上传中...
上传图片
插入图片