最近写程序时,往Oracle中插入数据,出现错误ORA-01036:非法的变量名/编号,代码如下:
DbConnection conn = GetCon();
conn.Open();
DbCommand cmd = conn.CreateCommand();
string sqlrt= "insert into T_User(Id,Name,Password,E_Mail) VALUES(@id,@name,@password,@email)";
cmd.CommandText = sqlrt;
cmd.Parameters.Add(new SqlParameter("@id", Convert.ToInt32(txtId.Text)));
cmd.Parameters.Add(new SqlParameter("@name", txtName.Text));
cmd.Parameters.Add(new SqlParameter("@password", txtPassword.Text));
cmd.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
int num = cmd.ExecuteNonQuery();
if (num > 0)
{
Response.Write("插入成功");
DataLoad();
}
解决方案是:
DbConnection conn = GetCon();
conn.Open();
DbCommand cmd = conn.CreateCommand();
string sqlrt= "insert into T_User(Id,Name,Password,E_Mail) VALUES(:id,:name,:password,:email)";
cmd.Parameters.Add(new OracleParameter(":id", Convert.ToInt32(txtId.Text)));
cmd.Parameters.Add(new OracleParameter(":name", txtName.Text));
cmd.Parameters.Add(new OracleParameter(":password", txtPassword.Text));
cmd.Parameters.Add(new OracleParameter(":email", txtEmail.Text));