当前位置:编程学习 > C#/ASP.NET >>

帮忙看一段C#代码,谢谢

private void btDel_Click(object sender, System.EventArgs e)
{
if (dataGrid1.DataSource!=null&&dataGrid1.CurrentRowIndex>=0&&dataGrid1[dataGrid1.CurrentCell]!=null)
{
oleConnection1.Open();
string sql = "select * from student where dormID='"+ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
OleDbCommand cmd = new OleDbCommand(sql,oleConnection1);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("删除宿舍'"+ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'失败,请先删该宿舍学生","提示");
dr.Close();

else
{
dr.Close();
sql="delete * from dorm where dormID not in (select distinct dormID from student) and dormID='"+ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";
cmd.CommandText=sql;

MessageBox.Show("删除宿舍'"+ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'成功","提示");
}
oleConnection1.Close();
}
}

运行完发现根本无法删除内容,求助! --------------------编程问答-------------------- 貌似你只是设置了sql语句没执行过? --------------------编程问答-------------------- 单步跟踪执行,添加try catch
sql="delete ...
cmd.ExecuteNonQuery --------------------编程问答-------------------- 嗯 确实只是用cmd.CommandText得到了SQL语句,没有执行,对了你何不将数据库的代码写一个类直接调用?
   直接把SQL语句传过去就可以了,比如你可以在这个类中设置4个方法用于 增删改查!
 
   --------------------编程问答-------------------- 设断点····跟断点 看是哪一步出了问题···· --------------------编程问答-------------------- LZ,你要去执行SQL语句啊 --------------------编程问答-------------------- 对额,你貌似只执行了第一个SQL语句

第二个delete没执行怎么删除 - -

--------------------编程问答-------------------- 看来有很多人还没有入门的 --------------------编程问答-------------------- sql="delete * from dorm where dormID not in (select distinct dormID from student) and dormID='"+ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim()+"'";

这一句没有执行啊!!!~~~ --------------------编程问答-------------------- 貌似执行if语句什么都没有做。 --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication18
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
                if (dataGrid1.DataSource != null && dataGrid1.CurrentRowIndex >= 0 && dataGrid1[dataGrid1.CurrentCell] != null)
                {
                    oleConnection1.Open();
                    string sql = "select * from student where dormID='" + ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "'";
                    OleDbCommand cmd = new OleDbCommand(sql, oleConnection1);
                    OleDbDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        MessageBox.Show("删除宿舍'" + ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "'失败,请先删该宿舍学生", "提示");
                        dr.Close();
                    }
                    else
                    {
                        dr.Close();
                        sql = "delete * from dorm where dormID not in (select distinct dormID from student) and dormID='" + ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "'";
                        cmd.CommandText = sql;

                        MessageBox.Show("删除宿舍'" + ds.Tables["dorm"].Rows[dataGrid1.CurrentCell.RowNumber][0].ToString().Trim() + "'成功", "提示");
                    }
                    oleConnection1.Close();
                }
            }
        }
    }
}



楼主只是用DataReader吧数据读进去

并没有删除 数据
MessageBox是谈消息的 并不能删除
dt.Rows.Remove(DataRow)  这个是用来从表中删除数据的 --------------------编程问答-------------------- 没有执行
cmd.ExecuteNonQuery
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,