Laziale
Laziale

Reputation: 8225

execute sql query from c# code for creating new sql table

I am trying to execute a sql query which will create a new table in sql database. When I am doing this:

string queryString = @"
CREATE TABLE [dbo].[peep_searchresults_live](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [SKU] [varchar](150) NULL,
    [ManufacturerBrand] [varchar](150) NULL,
);

The query gets executed just fine and the table gets created in the database.

But when I am trying this:

string queryString = @"
            SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[peep_searchresults_live](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [SKU] [varchar](150) NULL,
    [ManufacturerBrand] [varchar](150) NULL,            
 CONSTRAINT [PK_peep_searchresults_live] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO";

I am receiving error like this one:

enter image description here

Upvotes: 0

Views: 1594

Answers (3)

TheRealTy
TheRealTy

Reputation: 2429

You cannot use go, you need to split the command in to multiple sets.

http://social.msdn.microsoft.com/Forums/ar/csharpgeneral/thread/2907541f-f1cf-40ea-8291-771734de55f2

Upvotes: 0

pearcewg
pearcewg

Reputation: 9619

GO statements are not valid in ADO.net

Do you need all of those other qualifying statements?

You can break each into a separate command, and execute each command in turn with a single connection.

Upvotes: 0

James Hill
James Hill

Reputation: 61872

SQL server doesn't understand what GO is - only the query analyzer knows (eg. SSMS).

If you need to execute multiple commands, simply execute them in the proper order, one at a time.

Upvotes: 3

Related Questions