错误解决:ORA-01036

时间:2022-06-17 17:11:00

最近写程序时,往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));

在Oracle数据库中,参数替换不能使用"@",要使用":",将程序中的@替换成就可以解决这个问题了