如何使用.net为DB2的sql语句定义终止字符

时间:2011-06-23 15:25:02

标签: c# .net sql db2

我要执行以下

 public void ExecuteNonQuery(string script) {
        try {
            int returnCode;

            var builder = new DB2ConnectionStringBuilder {
                                                             UserID = Context.Parameters["USERID"],
                                                             Password =Context.Parameters["PASSWORD"],
                                                             Database =  Context.Parameters["DATABASE"],
                CurrentSchema =  Context.Parameters["CURRENTSCHEMA"]
            };

            using (var connection = new DB2Connection(builder.ConnectionString)) {

                using (var command = new DB2Command(script, connection)
                    ) {
                    command.CommandType = CommandType.Text;

                    connection.Open();

                    returnCode = command.ExecuteNonQuery();
                    File.WriteAllText(Context.Parameters["LOGFILE"], "Return Code -1 Successful : " + returnCode);
                }
            }
        }
        catch (Exception ex) {
            Trace.WriteLine(ex.StackTrace);
            throw ex;
        }
    }

我正在调用一个脚本,该脚本有多个语句以;;并且在文件末尾包含一个@符号。在db2命令行上,我可以使用db2 -td @ -f。我想知道如何将@符号定义为语句终止符,以便我可以从csharp执行脚本。 这是示例sql文件:

DROP PROCEDURE fred@

CREATE PROCEDURE fred ( IN name, IN id )
specific fred
language sql
b1: begin
      update thetable
      set  thename = name
      where table_id = id;
end: b1
@

grant execute on procedure inst.fred to user dbuser@

2 个答案:

答案 0 :(得分:0)

我不确定如何定义分隔符。但是,您可以尝试将整个文本拆分为C#中的单个语句,然后再执行一个语句。这样对您有用吗?

答案 1 :(得分:0)

这是一个老问题,但我有类似的问题,这里是解决方案:

--#SET TERMINATOR @
create or replace procedure test
begin
  declare line varchar(100);
  set line = 'hello db2 world!';
  call dmbs_output.put_line(line);
end@

然后就是:

$ db2 -f script.sql

它会正常工作。

相关问题