为什么这个插入不起作用?`

时间:2014-04-15 20:10:09

标签: c# oracle10g

实际上我的代码假设要检查course_choice_teacher表中是否有任何值。如果没有,则会在表中插入一些值。现在这次表中没有任何价值。所以dr2.Read()应该返回false并执行else部分。但它正在做相反的事情。如果你帮助我,我将非常高兴。

string oradb = "Data Source=localhost;User Id=system;Password=cse;";
OracleConnection conn = new OracleConnection(oradb);  // C#
conn.Open();

OracleCommand cmd2 = new OracleCommand();
cmd2.Connection = conn;
cmd2.CommandText = "select * from course_choice_teacher where teacher_id='"+teacher_home.st+"' and choice_no=1";
cmd2.CommandType = CommandType.Text;

OracleDataReader dr2 = cmd2.ExecuteReader();

if (dr2.Read())
{
   MessageBox.Show("Already Given");
}
else
{
   OracleCommand cmd = new OracleCommand();
   cmd.Connection = conn;
   cmd.CommandText = "insert into course_choice_teacher values('" + teacher_home.st + "','" + dataGridView1.CurrentRow.Cells["course_id"].Value.ToString() + "',1)";
   cmd.CommandType = CommandType.Text;

   OracleDataReader dr = cmd.ExecuteReader();
   dr.Read();
}

conn.Dispose();

1 个答案:

答案 0 :(得分:0)

datareader具有属性.HasRows来检查结果是否有某些行。这将是检查您打算做什么的正确方法。

if (dr2.HasRows)
{
    MessageBox.Show("Already Given");
}
else
{
  .........
}