我面临这个错误,告诉我我正在使用非法变量或数字,并且它在我的代码中突出显示了这一行
Line 34:rowsAffected = command.ExecuteNonQuery();
。我认为我的问题在于需要根据 Oracle 格式更改参数,但不确定。我确实用 @
替换了所有 p.Course_id
,然后是 ?p.course
,p_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;
/
我认为您的问题是由于在 Add 方法调用中使用无效枚举而引起的
如果运行此代码,您可能会注意到 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);
也许这就是您的答案:您应该从注释中删除所有字符冒号 (:)。有些驱动程序无法解析和排除注释中的内容,这是不正常的。