c# - .NET/甲骨文 : How to execute a script with DDL statements programmatically

标签 c# oracle ddl

我想在 C# 中对 Oracle 数据库进行一些编程模式操作。因此,我在一些基本问题上苦苦挣扎。

ddl sql 语句位于脚本文件中。我不想使用 sqlplus.exe,但我想在 ODP.NET 程序集 (System.Oracle.DataAccess) 之外使用 OracleCommand。这是我的脚本文件的示例:

脚本.sql:

DROP TABLE ABCDEF; 

DROP TABLE GHIJKL;

我想指出:

  • 脚本包含DDL语句(数据定义语言)
  • 脚本包含空行
  • 脚本包含多个语句

下面的代码应该执行我的脚本:

var content = File.ReadAllText("script.sql");

using (var oracleConnection = new OracleConnection(_connectionString))
{
     oracleConnection.Open();

     using (var command = new OracleCommand(content) { Connection = oracleConnection })
     {
          command.CommandType = CommandType.Text;
          command.ExecuteNonQuery();
     }
}

执行这段代码,我确实得到一个 oracle 错误:

Oracle.DataAccess.Client.OracleException: ORA-00911: 无效字符

我认为可能是语句的格式存在一些问题。任何提示表示赞赏。谢谢。

---编辑---

以简单的方式总结我的需求:我寻找一种方法来执行任何 sql/ddl 脚本,该脚本可通过 SQL Plus 以编程方式使用 C# 执行。

最佳答案

只需将它包裹在 BEGIN 和 END 中,它就会顺利运行

var content =string.Format("BEGIN {0} END;", File.ReadAllText("script.sql"));
using (var oracleConnection = new OracleConnection(_connectionString))            
{
  oracleConnection.Open();
  using (var command = new OracleCommand(content) { Connection = oracleConnection })
  {
       command.CommandType = CommandType.Text;
       command.ExecuteNonQuery();
  }
}

关于c# - .NET/甲骨文 : How to execute a script with DDL statements programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17725379/

相关文章:

c# - 无法理解此 MySQL 连接字符串

c# - 使用 DX Compositor 的 UWP App 实时模糊背景

c# - 如何将 CreateUserWizard 中额外添加的字段的信息保存在与 Membership 数据库不同的单独数据库中?

c# - Entity Framework : Mapping existing one-to-many relationship to Model

database - 即使应用程序崩溃,触发器是否保持原子性

sql - SQL 表与结构数组有何不同? (就用法而言,而不是实现)

oracle - 在 Oracle 过程中运行 DDL - ORA-06508 - 如何在运行时重新编译?

java - 有没有办法在WebSphere和OracleAS中自动化EAR部署?

sql - 如何确定数据库的类型 - 旧的遗留企业数据库

oracle - 创建具有 rowid 属性的对象类型