错误:sql命令未正确结束

时间:2011-03-10 06:03:10

标签: c# oracle

我必须搜索员工详细信息,该信息包含在3个表格中。我在查询查询中使用了联接,但是当我按下搜索按钮时显示错误:

sql command not properly ended

c#编码:

try {
  //Search Employee Details
  Oracle.DataAccess.Client.OracleConnection cn = new Oracle.DataAccess.Client.OracleConnection();

  cn.ConnectionString = "user id=system; password=system;";
  Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand();

  cmd.Connection = cn;
  //cn = new Oracle.DataAccess.Client.OracleConnection();
  cmd.CommandText = " select deposit.loanid, 
                             form1.empedoj, 
                             form1.empshare, 
                             sharecapital.shareint, 
                             sharecapital.loandt, 
                             sharecapital.loandeduc, 
                             sharecapital.dividend, 
                             sharecapital.sharetot 
                        from form1, 
                             deposit, 
                             sharecapital 
                       where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "'";  // shows sql command not properly ended

  Oracle.DataAccess.Client.OracleDataAdapter ada = new Oracle.DataAccess.Client.OracleDataAdapter(cmd);
  System.Data.DataTable dt = new DataTable();
  dt.Clear();
  ada.Fill(dt);

  //Display in Textbox
  if (dt.Rows.Count > 0) {
    txtlnid.Text = dt.Rows[0].ItemArray[0].ToString();
    admdate.Text = dt.Rows[0].ItemArray[1].ToString();
    txtadmamt.Text = dt.Rows[0].ItemArray[2].ToString();
    txtadmint.Text = dt.Rows[0].ItemArray[3].ToString();
    loandt.Text = dt.Rows[0].ItemArray[4].ToString();
    txtlnamt.Text = dt.Rows[0].ItemArray[5].ToString();
    txtlnint.Text = dt.Rows[0].ItemArray[6].ToString();
    txtsctot.Text = dt.Rows[0].ItemArray[7].ToString();
  }

  if (cn.State == ConnectionState.Closed) {
    cn.Open();
  }

  string str;
  str = cmd.ExecuteScalar().ToString();

  if (str != null) {
    MessageBox.Show("Record Found");
  } else {
    MessageBox.Show("ID not Match");
  }
} catch (Exception ex) {
  MessageBox.Show(ex.Message);
}

2 个答案:

答案 0 :(得分:4)

您的SQL语句变为

  SELECT DEPOSIT.LOANID,
         FORM1.EMPEDOJ,
         FORM1.EMPSHARE,
         SHARECAPITAL.SHAREINT,
         SHARECAPITAL.LOANDT,
         SHARECAPITAL.LOANDEDUC,
         SHARECAPITAL.DIVIDEND,
         SHARECAPITAL.SHARETOT
  FROM   FORM1, DEPOSIT, SHARECAPITAL
  WHERE  DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID =    '" + txtlnid.Text.Trim() + "'";

我怀疑它应该是:

  SELECT DEPOSIT.LOANID,
         FORM1.EMPEDOJ,
         FORM1.EMPSHARE,
         SHARECAPITAL.SHAREINT,
         SHARECAPITAL.LOANDT,
         SHARECAPITAL.LOANDEDUC,
         SHARECAPITAL.DIVIDEND,
         SHARECAPITAL.SHARETOT
  FROM   FORM1, DEPOSIT, SHARECAPITAL
  WHERE  DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID 
  AND    SHARECAPITAL.LOANID = '" + txtlnid.Text.Trim() + "'";

此外,如果没有正确的连接条件,您有一个3表连接,该查询极有可能返回笛卡尔积。

答案 1 :(得分:-2)

您是否尝试在查询字符串的末尾添加分号?

cmd.CommandText = " select deposit.loanid, form1.empedoj, form1.empshare,
sharecapital.shareint, sharecapital.loandt, sharecapital.loandeduc, 
sharecapital.dividend, sharecapital.sharetot from form1, deposit , 
sharecapital where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "';";