在执行命令过程中,发生了一个或多个错误。我在Oracle9i中建立了个hotelm数据库,里面有张表customer(customerid,customername,address,city,phone),用c#向数据库里添加数据,
代码如下: private void btnAdd_Click(object sender, System.EventArgs e)
{
try
{
drNew=this.ds.Tables[0].NewRow();   
drNew["CustomerID"]=this.txtID.Text.Trim();
drNew["CustomerName"]=txtName.Text.Trim();  
drNew["Address"]=txtAddress.Text.Trim();
drNew["City"]=txtCity.Text.Trim();
drNew["Phone"]=txtPhone.Text.Trim(); this.ds.Tables[0].Rows.Add(drNew);
this.cmdInsert=new OleDbCommand("insert into  hotel.customer(customerid,customername,address,city,phone) values(@customerid,@customername,@address,@city,@phone)",this.ocn);
this.oda.InsertCommand=cmdInsert; this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20);
  this.oda.InsertCommand.Parameters.Add("@customername",OleDbType.VarChar,20);
this.oda.InsertCommand.Parameters.Add("@address",OleDbType.VarChar,50);
this.oda.InsertCommand.Parameters.Add("@phone",OleDbType.VarChar,24);
this.oda.InsertCommand.Parameters.Add("@city",OleDbType.VarChar,15);  cmdInsert.Parameters["@customerid"].Value=txtID.Text.Trim();
cmdInsert.Parameters["@customername"].Value=txtName.Text.Trim();
cmdInsert.Parameters["@address"].Value=txtAddress.Text.Trim();
cmdInsert.Parameters["@city"].Value=txtCity.Text.Trim();
cmdInsert.Parameters["@phone"].Value=txtPhone.Text.Trim();
MessageBox.Show("还没执行插入操作!");

 this.cmdInsert.ExecuteScalar();// 有问题!!!
//ExecuteNonQuery();
 
MessageBox.Show("添加成功!");
this.dataGrid1.Refresh(); }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
           总是提示: 在执行命令过程中,发生了一个或多个错误。ORA-00936:缺少表达式
       (我用一条insert命令插入静态数据没有问题,通过文本框输入动态数据就总报错)

解决方案 »

  1.   

    一条一条跟踪一下,看你最后的sql是什么,应该这个地方的问题
      

  2.   

    insert into customer(customerid,customername,address,city,phone) values(@customerid,@customername,@address,@city,@phone)",this.ocn);
      

  3.   

    把@去掉,

    this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20);
    改为
    this.oda.InsertCommand.Parameters.Add(customerid,OleDbType.VarChar,20);
    试试你的这种写法是sql server 得。
      

  4.   

    写错了
    this.oda.InsertCommand.Parameters.Add(;customerid,OleDbType.VarChar,20);
      

  5.   

    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    try
    {
    drNew=this.ds.Tables[0].NewRow();   
    drNew["CustomerID"]=this.txtID.Text.Trim();
    drNew["CustomerName"]=txtName.Text.Trim(); 
    drNew["Address"]=txtAddress.Text.Trim();
    drNew["City"]=txtCity.Text.Trim();
    drNew["Phone"]=txtPhone.Text.Trim();this.ds.Tables[0].Rows.Add(drNew);
    this.cmdInsert=new OleDbCommand("insert into  hotel.customer(customerid,customername,address,city,phone) values(?,?,?,?,?)",this.ocn);
    this.oda.InsertCommand=cmdInsert;this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20);
     this.oda.InsertCommand.Parameters.Add("@customername",OleDbType.VarChar,20);
    this.oda.InsertCommand.Parameters.Add("@address",OleDbType.VarChar,50);
    this.oda.InsertCommand.Parameters.Add("@phone",OleDbType.VarChar,24);
    this.oda.InsertCommand.Parameters.Add("@city",OleDbType.VarChar,15); cmdInsert.Parameters["@customerid"].Value=txtID.Text.Trim();
    cmdInsert.Parameters["@customername"].Value=txtName.Text.Trim();
    cmdInsert.Parameters["@address"].Value=txtAddress.Text.Trim();
    cmdInsert.Parameters["@city"].Value=txtCity.Text.Trim();
    cmdInsert.Parameters["@phone"].Value=txtPhone.Text.Trim();
    MessageBox.Show("还没执行插入操作!"); this.cmdInsert.ExecuteScalar();// 有问题!!!
    //ExecuteNonQuery();
     
    MessageBox.Show("添加成功!");
    this.dataGrid1.Refresh();}
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }OleDb要用?
      

  6.   

    为什么要用oldDB?oraleClient有问题吗?
    把下面这些改一下
    this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20);
     this.oda.InsertCommand.Parameters.Add("@customername",OleDbType.VarChar,20);
    this.oda.InsertCommand.Parameters.Add("@address",OleDbType.VarChar,50);
    this.oda.InsertCommand.Parameters.Add("@phone",OleDbType.VarChar,24);
    this.oda.InsertCommand.Parameters.Add("@city",OleDbType.VarChar,15); cmdInsert.Parameters["@customerid"].Value=txtID.Text.Trim();
    cmdInsert.Parameters["@customername"].Value=txtName.Text.Trim();
    cmdInsert.Parameters["@address"].Value=txtAddress.Text.Trim();
    cmdInsert.Parameters["@city"].Value=txtCity.Text.Trim();
    cmdInsert.Parameters["@phone"].Value=txtPhone.Text.Trim();改成这样行不?
    this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20).Value = txtID.Text.Trim();
     this.oda.InsertCommand.Parameters.Add("@customername",OleDbType.VarChar,20).Value = txtName.Text.Trim();
    this.oda.InsertCommand.Parameters.Add("@address",OleDbType.VarChar,50).Value = 
    txtAddress.Text.Trim();
    this.oda.InsertCommand.Parameters.Add("@phone",OleDbType.VarChar,24).Value = txtCity.Text.Trim();
    this.oda.InsertCommand.Parameters.Add("@city",OleDbType.VarChar,15).Value = txtPhone.Text.Trim();
      

  7.   

    或把这句this.oda.InsertCommand=cmdInsert;写到MessageBox前来
      

  8.   

    OracleClientstring mysql = "insert into tblname (col1, col2) values (:col1, :col2)";cmd.Parameters.Add(":col1", OracleType.VarChar, 20);
    cmd.Parameters.Add(":col2", OracleType.VarChar, 20);cmd.Parameters[":col1"].Value = Text.Trim();
    ...
      

  9.   

    楼上的,你改后的代码跟改前的代码完全一样,只是形式上的差别,
    sqlclient,OleDb和OracleClient在sql语句中的占位符是不同的,㈠SqlClient要使用的是@开头的字符串,
    sql语句例如:insert into news(id,name) values(@id,@name)   
    在添加是必须使用cmd.Parameters.Add("@name",SqlType.Varchar,50)...这中格式㈡OleDb使用的是?作为占位符,
      insert into news(id,name) values(?,?)
      cmd.Parameters.Add("@name",SqlType.Varchar,50)...
      cmd.Parameters.Add("name",SqlType.Varchar,50)...
    使用上面两种方式都可以㈢OracleClient使用的是:开头的字符串
    insert into news(id,name) values(:ID,:name)
    cmd.Parameters.Add(":name",SqlType.Varchar,50)...
      

  10.   

    不好意思上面,cmd.Parameters.Add("@name",SqlType.Varchar,50)...中的数据类型写错了,
    应该是各自的数据类型sqlclient->sqltype,oledb->oleDbType,oracleclient->oralcetype
      

  11.   

    执行更新操作时候用以下 OleDbCommand对象时,都不能成功执行 this.cmdUpdate.ExecuteNonQuery()命令this.cmdUpdate=new OleDbCommand("update  hotel.customer set customername= ? ,address= ?  WHERE customerid = ? ",this.ocn);
    或者
    this.oda.UpdateCommand=new OleDbCommand("update  hotel.customer set customername='" + txtName.Text.Trim() + " ' ,address='"+ txtAddress.Text.Trim()+"'  WHERE customerid ='"+ txtID.Text.Trim() +" ' ",this.ocn);