在执行命令过程中,发生了一个或多个错误。我在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命令插入静态数据没有问题,通过文本框输入动态数据就总报错)
代码如下: 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命令插入静态数据没有问题,通过文本框输入动态数据就总报错)
把
this.oda.InsertCommand.Parameters.Add("@customerid",OleDbType.VarChar,20);
改为
this.oda.InsertCommand.Parameters.Add(customerid,OleDbType.VarChar,20);
试试你的这种写法是sql server 得。
this.oda.InsertCommand.Parameters.Add(;customerid,OleDbType.VarChar,20);
{
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要用?
把下面这些改一下
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();
cmd.Parameters.Add(":col2", OracleType.VarChar, 20);cmd.Parameters[":col1"].Value = Text.Trim();
...
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)...
应该是各自的数据类型sqlclient->sqltype,oledb->oleDbType,oracleclient->oralcetype
或者
this.oda.UpdateCommand=new OleDbCommand("update hotel.customer set customername='" + txtName.Text.Trim() + " ' ,address='"+ txtAddress.Text.Trim()+"' WHERE customerid ='"+ txtID.Text.Trim() +" ' ",this.ocn);