浅析C#绑定变量和匿名块获取获取序列当前值
2010-09-30 20:58:52 来源:WEB开发网我们将谈到C#绑定变量和匿名块获取获取序列当前值,首先需要在Oracle中进行一系列的操作,接下来是C#绑定变量的具体操作。
1.在Oracle中建立表、序列、触发器:
SQL> create table TESTTAB
(
ID NUMBER,
NAME VARCHAR2(10)
);
表被创建
SQL> create sequence TESTTABSEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
序列被创建
SQL> create or replace trigger Tr_testtabid
before insert on testtab for each row
begin
SELECT TESTTABSEQ.NEXTVAL into :NEW.id from dual;
end Tr_testtabid;
/
触发器被创建
2.主要的C#绑定变量代码:
using ORAC = System.Data.OracleClient;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string str_Sql = @"begin insert into testtab(name) values('test'); select TESTTABSEQ.Currval into :ID from dual; end;";
ORAC.OracleCommand cmd= new ORAC.OracleCommand(str_Sql,this.oracleConnection1);
ORAC.OracleParameter parm = new ORAC.OracleParameter("ID",ORAC.OracleType.Number);
parm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm);
if(this.oracleConnection1.State == System.Data.ConnectionState.Closed)
{
this.oracleConnection1.Open();
}
cmd.ExecuteNonQuery();
this.textBox1.Text = cmd.Parameters[0].Value.ToString();
}
catch(Exception ex)
{
MessageBox.Show("发生错误!";
}
finally
{
this.oracleConnection1.Close();
}
}
更多精彩
赞助商链接