问题
I am trying to perform update action in Visual Studio 2010 Ultimate with Windows form as the front end and Oracle 11g express as the back end. I am using C# to code this.
private void update_student(string STUDENT_ID, string STUDENT_NAME, string STUDENT_ADDRESS)
{
con.Open();
String sql = "UPDATE STUDENT SET STUDENT_NAME = :STUDENT_NAME, STUDENT_ADDRESS= :STUDENT_ADDRESS WHERE STUDENT_ID= :STUDENT_ID";
OracleCommand query = new OracleCommand(sql, con);
OracleParameter[] updatestud = new OracleParameter[3];
updatestud[0] = query.Parameters.Add("STUDENT_ID", OracleDbType.Varchar2, STUDENT_ID, ParameterDirection.Input);
updatestud[1] = query.Parameters.Add("STUDENT_NAME", OracleDbType.Varchar2, STUDENT_NAME, ParameterDirection.Input);
updatestud[2] = query.Parameters.Add("STUDENT_ADDRESS", OracleDbType.Varchar2, STUDENT_ADDRESS, ParameterDirection.Input);
query.ExecuteNonQuery();
MessageBox.Show("Row Updated");
con.Close();
}
After performing insertion and retrieval actions I am now trying out Update query.
I couldn't understand the flow of parameters and values of the following code that I have used in my application.
I get the following error in the query.ExecuteNonQuery(); line:
ORA-01722: invalid number
Any help will be really appreciated.
回答1:
From ORA-01722
The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.
This error may cause probably you try to define your STUDENT_ID as a OracleDbType.Varchar2.
I'm not sure how long your STUDENT_ID but you can use OracleDbType.Int32 instead of.
Like;
updatestud[0] = query.Parameters.Add("STUDENT_ID", OracleDbType.Int32 , STUDENT_ID, ParameterDirection.Input);
Check out OracleType enumeration for more details.
Specifies the data type of a field or property for use in an OracleParameter.
回答2:
I am thinking STUDENT_ID may be integer. you are using it as Varchar2, which seems to be the cause of error.
ORA-01722: invalid number Cause: The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.
回答3:
Hi people :) I found the answer myself. Just thought of sharing with you all.
There are 2 methods to accomplish this.
Method 1:
OracleParameter[] updatestud = new OracleParameter[3];
updatestud[0] = query.Parameters.Add(":STUDENT_NAME", OracleDbType.Varchar2, STUDENT_NAME, ParameterDirection.Input);
updatestud[1] = query.Parameters.Add(":STUDENT_ADDRESS", OracleDbType.Varchar2, STUDENT_ADDRESS, ParameterDirection.Input);
updatestud[2] = query.Parameters.Add("STUDENT_ID", OracleDbType.Int32, STUDENT_ID, ParameterDirection.Input);
Method 2:
String sql = "UPDATE STUDENT SET STUDENT_NAME = :STUDENT_NAME, STUDENT_ADDRESS= :STUDENT_ADDRESS WHERE STUDENT_ID= :STUDENT_ID";
OracleCommand query = new OracleCommand(sql, con);
OracleParameter p_studid = new OracleParameter();
OracleParameter p_studname = new OracleParameter();
OracleParameter p_studaddr = new OracleParameter();
p_studname.OracleDbType = OracleDbType.Varchar2;
p_studname.Value = TxtName.Text;
query.Parameters.Add(p_studname);
p_studaddr.OracleDbType = OracleDbType.Varchar2;
p_studaddr.Value = TxtAddress.Text;
query.Parameters.Add(p_studaddr);
p_studid.OracleDbType = OracleDbType.Int32;
p_studid.Value = TxtId.Text;
query.Parameters.Add(p_studid);
You may use any of these methods just remember one thing... Parameters should always be added in the order of the sql. And of course, one must also take care of the OracleDBType.
Also refer to dis page if in case you have doubts-http://docs.oracle.com/cd/E17781_01/appdev.112/e18751/building_odp.htm
Thank you all for yur suggestions! :)
来源:https://stackoverflow.com/questions/15363303/error-in-update-query-visual-studio-2010-oracle-11g-express-edition