Oracle Entity Framework 将参数传递给命令文本
我有一个表测试
CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(30 BYTE)
)
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
ALTER TABLE TEST ADD (
PRIMARY KEY
(ID)
现在我想从页面插入文本框中的值。
代码如下,
protected void ASPxButton1_Click(object sender, EventArgs e)
{
//string name = Convert.ToString(ASPxTextBox1.Text);
int retVal = 0;
string providerName = "Oracle.DataAccess.Client";
string constr =
@"User Id=scott;Password=tiger;Data Source=orcl;enlist=true";
// Get the provider factory.
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
try
{
using (scope txn = new scope())
{
using (DbConnection conn1 = factory.CreateConnection())
{
conn1.ConnectionString = constr;
conn1.Open();
// Create a command to execute the sql statement.
DbCommand cmd1 = factory.CreateCommand();
cmd1.Connection = conn1;
cmd1.CommandText = @"insert into test ( [NAME] ) values (:name)";
//OracleParameter Nameprm = cmd1.Parameters.Add("name" , OracleDbType.Varchar2, 10 );
//Nameprm.Direction = ParameterDirection.Input;
//Nameprm.Value = ASPxTextBox1;
var name = Convert.ToString(ASPxTextBox1);
var parameter = factory.CreateParameter();
parameter.DbType = System.Data.DbType.String;
parameter.ParameterName = "@name";
parameter.Value = name;
cmd1.Parameters.Add(parameter);
retVal = cmd1.ExecuteNonQuery();
//Console.WriteLine("Rows to be affected by cmd1: {0}", retVal);
// Close the connection and dispose the command object.
conn1.Close();
conn1.Dispose();
cmd1.Dispose();
}
txn.SaveChanges();
}
}
catch (Exception ex)
{
throw;
}
}
}
但我总是收到 ORA-00928:缺少 SELECT 关键字。但如果我直接给出值,那么它会插入数据。我尝试通过多种方式解决它仍然没有运气。非常感谢您的帮助,因为我被困在这里。先感谢您。
i have a table test
CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(30 BYTE)
)
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
ALTER TABLE TEST ADD (
PRIMARY KEY
(ID)
Now I want to insert from a page with values from textbox.
The code is given below
protected void ASPxButton1_Click(object sender, EventArgs e)
{
//string name = Convert.ToString(ASPxTextBox1.Text);
int retVal = 0;
string providerName = "Oracle.DataAccess.Client";
string constr =
@"User Id=scott;Password=tiger;Data Source=orcl;enlist=true";
// Get the provider factory.
DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
try
{
using (scope txn = new scope())
{
using (DbConnection conn1 = factory.CreateConnection())
{
conn1.ConnectionString = constr;
conn1.Open();
// Create a command to execute the sql statement.
DbCommand cmd1 = factory.CreateCommand();
cmd1.Connection = conn1;
cmd1.CommandText = @"insert into test ( [NAME] ) values (:name)";
//OracleParameter Nameprm = cmd1.Parameters.Add("name" , OracleDbType.Varchar2, 10 );
//Nameprm.Direction = ParameterDirection.Input;
//Nameprm.Value = ASPxTextBox1;
var name = Convert.ToString(ASPxTextBox1);
var parameter = factory.CreateParameter();
parameter.DbType = System.Data.DbType.String;
parameter.ParameterName = "@name";
parameter.Value = name;
cmd1.Parameters.Add(parameter);
retVal = cmd1.ExecuteNonQuery();
//Console.WriteLine("Rows to be affected by cmd1: {0}", retVal);
// Close the connection and dispose the command object.
conn1.Close();
conn1.Dispose();
cmd1.Dispose();
}
txn.SaveChanges();
}
}
catch (Exception ex)
{
throw;
}
}
}
But i am always getting ORA-00928: missing SELECT keyword. but if i give values directly then its inserting data. I am trying to solve it in many ways still no luck. Your help will be very much appreciated as i am stuck here. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@competent_tech 的答案看起来不错,作为一个额外的想法:
在我的上一份工作中,我们使用了一个巨大的 Oracle 数据库来实现所有持久性,并且我对此类问题感到无尽的悲伤。当我停止使用内置的 ADO.Net 驱动程序并使用 Oracle 提供的驱动程序(按照 Microsoft 此处)。
您可以获取适用于您的 Oracle 版本的 Oracle Data Provider for .Net (ODP.Net) 这里。
如果您已经在使用 ODP.Net,我真诚地道歉,从代码片段中很难看出:)
如果名称不是问题,那么可能是参数类型 (OracleDBType.VarChar2,尽管这应该通过 DbType 为您推断)或参数方向(ParameterDirection.InputOnly 参数?)
我希望可能有所帮助:)
@competent_tech 's answer looks good, as an additional thought:
In my last job-but-one we used a massive Oracle database for all our persistence, and I had no end of grief with this sort of problem. Most of them were solved when I stopped using the built-in ADO.Net drivers and used the Oracle provided drivers (as recommended by Microsoft here).
You can get the Oracle Data Provider for .Net (ODP.Net) for your version of Oracle here.
If you're already using the ODP.Net my sincere apologies, it's a little hard to tell from the code snippet :)
If the name isn't the problem, then it may be with the parameter type (OracleDBType.VarChar2, although this should be inferred for you by DbType) or the parameter direction (ParameterDirection.InputOnly parameter?)
I hope that might help :)
我认为您的参数名称不正确。
:
尝试改变
I don't think that your parameter name is correct.
Try changing:
to