none
数据库删除问题 RRS feed

  • 问题

  •  
    
                if (dataGridView1.CurrentCell!= null)
    
                {
    
                    string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JoblD not in (select distinct Table2.JobName from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";
    
                    SqlCommand com=new SqlCommand (str,con);
    
                    SqlDataReader dr;
    
                    dr = com.ExecuteReader();
    
                    if (!dr.Read())
    
                    {
    
                        MessageBox.Show("删除工种'" + ds.Tables[0].Rows[dataGridView1.CurrentCell.RowIndex][1].ToString().Trim() + "'失败,请先删除与此工种的相关员工", "消息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    
                        dr.Close();
    
                    }
    
                }
    
    
    出现错误为,无法将表2中的inverchar "JobName"字段转换为INT型,是不是我写错了什么?怎么看都没错额.....

    • 已编辑 heiye178 2011年9月3日 4:57
    2011年9月3日 4:56

答案

  • 请仔细看我下划线加粗的部分!

      string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JoblD not in (select distinct Table2.JobName from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";

    你怎么可以把jobId和JobName相比较呢?

    建议把第二个下划线部分改成:

    Table2.JobId


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年9月3日 5:01
    版主
  • 因为JoblD类型为int,JobName类型为nvarchar

    1. string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JoblD not in (select distinct Table2.JobID from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";
    2. string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JobName not in (select distinct Table2.JobName from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";

    看需求,请自取一个

    另外,好像没看到有Delete语句(似乎不太符合题意?)


    Shadowと愉快なコード達

    2011年9月3日 10:33

全部回复

  • 请仔细看我下划线加粗的部分!

      string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JoblD not in (select distinct Table2.JobName from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";

    你怎么可以把jobId和JobName相比较呢?

    建议把第二个下划线部分改成:

    Table2.JobId


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年9月3日 5:01
    版主
  • 因为JoblD类型为int,JobName类型为nvarchar

    1. string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JoblD not in (select distinct Table2.JobID from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";
    2. string str = "select JobName from Table2 where JoblD=" + ds.Tables[0].Rows[dataGridView1.CurrentRow.Index][0].ToString().Trim()+ "and JobName not in (select distinct Table2.JobName from Table3 inner join Table2 on Table3.JobName = Table2.JobName)";

    看需求,请自取一个

    另外,好像没看到有Delete语句(似乎不太符合题意?)


    Shadowと愉快なコード達

    2011年9月3日 10:33