【问题标题】:Execute Alter Tablespace Statement from C# Visual Studio without stored procedure在没有存储过程的情况下从 C# Visual Studio 执行 Alter Tablespace 语句
【发布时间】:2019-05-16 13:28:03
【问题描述】:

我需要从 C# 前端程序添加数据文件或调整数据文件的大小,而数据库中没有任何存储过程。

如何做到这一点?

Label1.text  =Tablespace name
textbox1.text = location of datafile
textbox2.text = name of the datafile

这里是示例代码,但是当我运行它时出现错误:

ORA-00900: 无效的 SQL 语句

代码:

private void button1_Click(object sender, EventArgs e)
{
    string sqla = @"alter tablespace '" + label1.Text + "' add datafile '" + textBox1.Text + "' size " + textBox2.Text +"M";
    OracleConnection conn3 = new OracleConnection(); 
    conn3.ConnectionString = connectform.connectionString;
    conn3.Open(); 
    OracleCommand cmd3 = new OracleCommand("sqla", conn3);
    cmd3.CommandType = CommandType.Text;
    cmd3.ExecuteNonQuery();
    conn3.Close();
}

【问题讨论】:

  • Visual Studio 是一个 IDE。它不会执行您的代码。因此,visual-studio 标记仅用于与 Visual Studio 应用程序相关的问题,而不是您使用它编写的代码。
  • label1.TexttextBox1.TexttextBox2.Text 的值是多少?完成的查询是什么?您没有清理这些输入字段的值,因此我假设您输入的某些文本会破坏您的查询。由于您没有提供,我怀疑有人可以帮助您。
  • 为什么要将表空间名称放在单引号中(alter tablespace 'tablespace_name'...)?
  • 你的内容真的是“数据文件的位置”和“数据文件的名称”吗?
  • 为什么 sqla 在引号中:OracleCommand cmd3 = new OracleCommand("sqla", conn3);

标签: c# oracle plsql odbc


【解决方案1】:

驱动程序有ExecuteOracleNonQuery 方法吗?可能值得尝试,而不是 ExecuteNonQuery

如果运气不好...

对此我不确定,但我怀疑驱动程序可能无法识别ALTER TABLESPACE

如果是这种情况,您可以尝试将其包装在 PLSQL 块中并使用 EXECUTE IMMEDIATE,类似:

private void button1_Click(object sender, EventArgs e)
{
    string sqla = @"BEGIN EXECUTE IMMEDIATE 'alter tablespace " + label1.Text + " add datafile '" + textBox1.Text + "' size " + textBox2.Text +"M'; END;";
    OracleConnection conn3 = new OracleConnection(); 
    conn3.ConnectionString = connectform.connectionString;
    conn3.Open(); 
    OracleCommand cmd3 = new OracleCommand("sqla", conn3);
    cmd3.CommandType = CommandType.Text;
    cmd3.ExecuteNonQuery();
    conn3.Close();
}

这从那里“隐藏”了ALTER TABLESPACE 驱动程序,而是呈现一个 PLSQL 块,它应该很高兴地处理它。

【讨论】:

    猜你喜欢
    • 2013-04-30
    • 2019-08-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2021-05-27
    相关资源
    最近更新 更多