System.Data.OracleClient.OracleException: ORA-01036: 非法变量名称/



我遇到了这个错误,告诉我我正在使用非法变量或数字,它在我的代码Line 34:rowsAffected = command.ExecuteNonQuery();中突出显示了这一行。我想我在需要根据 Oracle 格式更改的参数中存在问题,但不确定。我确实用p.Course_id替换了所有@,然后是?p.coursep_course_id就像我在oracle中的存储过程中所做的那样,但它们都不起作用。我仍然收到同样的错误。请帮我解决这个问题。谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

public class PostForum
{
    public static int INSERTforum(int course_Id, string question, string posterName, DateTime blog_date)
    {
        int rowsAffected = 0;
        using (OracleConnection connection = ConnectionManager.GetDatabaseConnection())
        {
            OracleCommand command = new OracleCommand("INSERTforum", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@course_Id", SqlDbType.Int).Value = course_Id;
            command.Parameters.Add("@question", SqlDbType.VarChar).Value = question;
            command.Parameters.Add("@posterName", SqlDbType.VarChar).Value = posterName;
            command.Parameters.Add("@blogdate", SqlDbType.DateTime).Value = blog_date;

            rowsAffected = command.ExecuteNonQuery();
        }
        return rowsAffected;
    }
}

这是我的存储过程

CREATE OR REPLACE PROCEDURE INSERTforum(
       p_course_id IN forum.COURSE_ID%TYPE,
       p_question IN forum.QUESTION%TYPE,
       p_postername IN forum.POSTERNAME%TYPE,
       p_blogdate IN forum.BLOG_DATE%TYPE)
AS
BEGIN
  INSERT INTO forum ("COURSE_ID", "QUESTION", "POSTERNAME", "BLOG_DATE") 
  VALUES (p_course_id, p_question,p_postername, p_blogdate);
  COMMIT;
END;
/

我认为您的问题是通过在添加方法调用中使用无效枚举引起的

如果运行此代码,您可能会注意到 Int32 的 OracleType 与 SqlDbType 不同

OracleType e = OracleType.Int32;
int i = (int)e;
Console.WriteLine(i.ToString());   // Output = 28
SqlDbType z = SqlDbType.Int;
i = (int)z;
Console.WriteLine(i.ToString());   // Output = 8

因此,我建议为您的 ADO.NET 提供者使用正确的枚举。

有趣的是,使用 SqlDbType 而不是 OracleType 调用 Add 是可以接受的,并且不会引发编译器时间错误。发生这种情况是因为 Add 方法具有接受对象作为第二个参数的重载(它用于在构造参数时直接传递值)。

另一种方法是使用AddWithValue OracleParameterCollection

   command.Parameters.AddWithValue("@course_Id", course_Id);
   command.Parameters.AddWithValue("@question", question);
   command.Parameters.AddWithValue("@posterName", posterName);
   command.Parameters.AddWithValue("@blogdate", blog_date);

最新更新