[英]Running oracle script in c#
I have a script which contains some SQL code to create tables. 我有一个脚本,其中包含一些用于创建表的SQL代码。 this script contains something like this:
该脚本包含以下内容:
CREATE TABLE Customer
(
CustomerID integer PRIMARY KEY,
FirstName varchar(32) NOT NULL,
LastName varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
)
CREATE TABLE Supplier
(
SupplierID varchar(8) PRIMARY KEY,
Name varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
)
I used this code to run this script: 我使用以下代码运行此脚本:
try
{
var content = File.ReadAllText("c:\\users\\vahid\\desktop\\DBAssignment5\\A3\\SchemaSetUp.sql");
using (var command = new OracleCommand(content) { Connection = conn })
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("Something's wrong\n");
Console.WriteLine(ex.ToString());
}
When the script contains the code of creating just one table, it works well, but when I add codes to create more than one table, I face this exception: 当脚本包含仅创建一个表的代码时,它可以很好地工作,但是当我添加代码以创建多个表时,我将遇到以下异常:
Can anybody please tell me what's wrong and what should I do to fix this problem? 谁能告诉我什么地方出了问题,我应该怎么做才能解决这个问题?
Thank you 谢谢
If your script contains only CREATE TABLE, CREATE VIEW or GRANT commands you can use CREATE SCHEMA command: 如果您的脚本仅包含CREATE TABLE,CREATE VIEW或GRANT命令,则可以使用CREATE SCHEMA命令:
CREATE SCHEMA AUTHORIZATION husqvik
CREATE TABLE Customer
(
CustomerID integer PRIMARY KEY,
FirstName varchar(32) NOT NULL,
LastName varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
)
CREATE TABLE Supplier
(
SupplierID varchar(8) PRIMARY KEY,
Name varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
);
Good thing on the fact that this is single atomic SQL command is that all tables/views/grants are created or none. 关于这是单个原子SQL命令的事实是一件好事,即所有表/视图/批处理都已创建或没有创建。 Interesting is that SQL*Plus doesn't recognize this statement and executes only the second
CREATE TABLE
command. 有趣的是,SQL * Plus无法识别此语句,仅执行第二个
CREATE TABLE
命令。
Otherwise you need to split the script into multiple commands or execute as anonymous block: 否则,您需要将脚本拆分为多个命令或作为匿名块执行:
BEGIN
EXECUTE IMMEDIATE
'CREATE TABLE Customer
(
CustomerID integer PRIMARY KEY,
FirstName varchar(32) NOT NULL,
LastName varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
)';
EXECUTE IMMEDIATE
'CREATE TABLE Supplier
( SupplierID varchar(8) PRIMARY KEY,
Name varchar(32) NOT NULL,
Street varchar(32),
Phone varchar(32)
)';
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.