如何使用.net为DB2定义sql语句的终止字符
我正在尝试执行以下命令,
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@
I am tring to execute the following
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;
}
}
I am calling a script that has multiple statements ending in ;'s and at the end of the file it contains an @ symbol. On a db2 command line I could use the db2 -td@ -f . I would like to know how to define the @ symbol as the statement terminator so I could execute the script from csharp.
Here is example sql file :
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@
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我们如何定义分隔符。但是,您可以尝试将整个文本拆分为 C# 中的单独语句,然后一个接一个地执行语句。这对你有用吗?
I am not sure how we can define delimiters. But, you could try to split the whole text into individual statements in C# and then execute one statement after the other. Would that work for you?
这是一个老问题,但我遇到了类似的问题,这是解决方案:
然后就是:
它会正常工作。
That's an old question, but I had similar problem and here is the solution:
and then just:
and it will work fine.