我有一個表測試甲骨文實體框架參數傳遞到命令文本
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關鍵字。但如果我直接給它的值,然後插入數據。我試圖用很多方式解決它,但仍然沒有運氣。當我卡在這裏時,您的幫助將非常感激。先謝謝你。
卡爾林奇先生感謝您分享您的經驗。從我的項目開始,我總是嘗試使用odp.net(odac)。以下是我的庫使用System;使用System.Data的 – tanvir 2011-12-22 07:05:41
; using System.Data.Common;使用Oracle.DataAccess.Client的 ; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; – tanvir 2011-12-22 07:06:52
是的,我試着用或不用parameter.direction。在兩種情況下,它的工作。最後我遇到了問題。它的行var name = Convert.ToString(ASPxTextBox1);那應該是ar名稱= Convert.ToString(ASPxTextBox1.text);. Lollz我的想法非常令人毛骨悚然。在按Tab之前需要查看。無論如何非常感謝。 – tanvir 2011-12-22 07:09:44