哪位高手能告述我如何在 C# 中执行Oracle的PL/SQL语句?

zahuifan 2005-06-10 04:51:31
下面的PL/SQL 语句在 SQL PLUS 中是可以正常执行的,可是我把它放在C#中执行,就会出现错误

ORA-06550: 第 1 行, 第 6 列: PLS-00103: 出现符号 ""在需要下列之一时: begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge <a single-quoted SQL string> pipe

代码为:
OracleConnection conn = new OracleConnection("user id=performance_test;data source=tst;password=pt");

StringWriter cmdText = new StringWriter();
cmdText.WriteLine("begin");
cmdText.WriteLine(" declare");
cmdText.WriteLine(" name varchar2(50); ");
cmdText.WriteLine(" begin");
cmdText.WriteLine(" name := 'jcl';");
cmdText.WriteLine(" end;");
cmdText.WriteLine("end;");

OracleCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdText.ToString();

OracleString rowid = new OracleString();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
//cmd.ExecuteOracleNonQuery(out rowid);
cmd.Connection.Close();

把PL/SQL语句改为
declare
name varchar2(50);
begin
name := 'jcl';
end;
也不行。高手赐教。
...全文
284 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zahuifan 2005-06-14
  • 打赏
  • 举报
回复
怎么没一个人会,高手在那里?
分只给真正的高手。
rolandzhang 2005-06-13
  • 打赏
  • 举报
回复
估计是单引号、分号的识别问题。
如果是单个的sql语句,可以,用.CommandText,如果是多条sql语句或者是pl/sql块,还是用存储过程写好,用C#调用(可以传入、传出参数)
mathsword 2005-06-10
  • 打赏
  • 举报
回复
用存储过程!
chenzhanyiczy 2005-06-10
  • 打赏
  • 举报
回复
就算行,这样写有什么用?
conan19771130 2005-06-10
  • 打赏
  • 举报
回复
如果oracle可以,为何不试试存储过程啊
lovefootball 2005-06-10
  • 打赏
  • 举报
回复
你为啥不把这些命令写成存储过程啊?然后直接调用存储过程多方便啊?
lovefootball 2005-06-10
  • 打赏
  • 举报
回复
你把string 改成
string str = "eclare name varchar2(50); Begin name := 'jcl'; end";
试试

我在plsql里面
declare
name varchar2(50);
begin
name := 'jcl';
end;

这样写是可以的,没有错误
xinbin1122 2005-06-10
  • 打赏
  • 举报
回复
upup
ahyf 2005-06-10
  • 打赏
  • 举报
回复
:=
=
??
qinjin2000 2005-06-10
  • 打赏
  • 举报
回复
呵呵,不知道啊
共同学习
C# 访问Oracle示例+PL/SQL+存储过程+触发器 完整示例 测试可用 --PL/SQL基础1 declare begin dbms_output.('不输出不换行'); dbms_output.put_line('输出并换行'); end; --PL/SQL基础2 declare dig number(20,2); begin select avg(price) into dig from products; dbms_output.put_line('电子产品的平均价格是'||dig); end; --PL/SQL基础3 根据产品编号获得产品对象 --pname products.name%type; pname变量的类型与products.name列的类型一样 declare pid constant products.id%type:=1; --定义常量,初值1 pname products.name%type; pdate products.adddate%type; begin --pid:=1; select name,adddate into pname,pdate from products where id=pid; dbms_output.put_line('产品名称是:'||pname||',日期'||pdate); end; --PL/SQL基础4 根据产品编号获得产品对象 --obj products%rowtype; obj与products表的单行类型一样,可以通过点运算取值obj.price declare obj products%rowtype; begin select * into obj from products where id=&编号; dbms_output.put_line('产品名称是:'||obj.name||',价格:'||obj.price); end; --PL/SQL基础5 条件if declare vid products.id%type; vprice products.price%type; begin vid:=&编号; select price into vprice from products where id=vid; if vprice100 and vprice<=1000 then dbms_output.put_line('价格在100—1000之间'); else dbms_output.put_line('价格在1000以上'); end if; end; --PL/SQL基础5 多条件case begin case '&等级' when 'A' then dbms_output.put_line('优秀'); when 'B' then dbms_output.put_line('合格'); when 'C' then dbms_output.put_line('不合格'); end case; end; select id, name, typeid, price, adddate from products create table students( Id int primary key, sex int ) insert into students select 1,1 from dual union select 2,0 from dual union select 3,1 from dual union select 4,0 from dual union select 5,1 from dual insert into students(Id) values(6) select * from students; select translate(translate('1心1意 3心2意','1','一'),'3','三') from dual; select id,nvl(translate(translate(sex,1,'女'),0,'男'),'未知') from students; select id,case as 性别 from( sele

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧