-
Dec 20th, 2019, 01:37 PM
#1
Thread Starter
Lively Member
How do I detail which database to create a view in SQL using vb.net?
My first attempt: (obviously not the whole query, but this is where its having issues)
Code:
Dim viewCreationString As String = "USE [" & databaseName & "];
CREATE VIEW [dbo].[" & ProjectNameTextBox.Text & "_view] AS SELECT DISTINCT "
Error: 'CREATE VIEW' must be the first statement in a query batch.
My second attempt: (again not the whole query, but this is where its having issues)
Code:
Dim viewCreationString As String = "USE [" & databaseName & "]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[" & ProjectNameTextBox.Text & "_view] AS SELECT DISTINCT "
Error: Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
-
Dec 20th, 2019, 01:56 PM
#2
Thread Starter
Lively Member
Re: How do I detail which database to create a view in SQL using vb.net?
Turns out using the database prefix later on in the query helped determined which database the view was to be created in.
This worked:
Code:
Dim viewCreationString As String = " CREATE VIEW [dbo].[" & ProjectNameTextBox.Text & "_view] AS SELECT DISTINCT "
-
Dec 20th, 2019, 03:19 PM
#3
Re: How do I detail which database to create a view in SQL using vb.net?
Well, if you're connected to the correct database in the first place, then it'll be created in that database and then you don't need to worry about it.
And I'm not sure what you meant by
database prefix later on in the query helped determined which database the view was to be created in.
because what prefix you use in the query has no bearing on where it gets created... what has bearing is the context at the time the query is created.
-tg
-
Dec 20th, 2019, 07:12 PM
#4
Thread Starter
Lively Member
Re: How do I detail which database to create a view in SQL using vb.net?
That's weird because I am connected with the sever admin creds, which has access to all databases and its still creating the view in the correct database...
-
Dec 20th, 2019, 08:36 PM
#5
Re: How do I detail which database to create a view in SQL using vb.net?
You don't use "GO" in ADO.NET. That is specific to SSMS I believe.
What does your connection string look like? Are you specifying an Initial Catalog value? If so, that is the database that any SQL you execute over that connection will affect. If you don't specify a database, I believe that you connect to the default database for the login. You have to call ChangeDatabase on your connection in order to change the database you're connected to.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|