0

at my project having 2Databases and my server is using SQL Server 2012 Management Studio. And now i would like to connect to one of the database and made a new table in runtime. But i'm having a problem.

Database Not Exist

but when i check to my location. It exist and with same name..

here is my code : enter image description here

Private Sub VBcreate()
        Dim sqlcon2 As New SqlConnection(vbcon)
        MainMenu.lblStatus.Text = "Creating Virtual Book"
        sqlstr = ("USE [VIRTUALBOOK] " & _
                  "GO() " & _
                  "SET ANSI_NULLS ON" & _
       " GO() " & _
        "SET QUOTED_IDENTIFIER ON" & _
        " GO() " & _
     "CREATE TABLE [dbo].[" & tbEmail.Text & "](" & _
                      "[ID] [ntext] NULL, " & _
    "[REMARKS] [NCHAR](6) NULL " & _
") ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]")

        Try
            sqlcon2.Open()
            cmd = New SqlCommand(sqlstr, sqlcon2)
            cmd.CommandType = CommandType.Text
            cmd.ExecuteNonQuery()
            sqlcon2.Close()
            MainMenu.lblStatus.Text = "Command Saved to Server"
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            sqlcon2.Close()
        End Try

    End Sub
Steve
  • 213,761
  • 22
  • 232
  • 286
Kasnady
  • 2,249
  • 3
  • 25
  • 34
  • 1
    Are you sure that the database is on the same server. Please try to get database list on the same connection http://stackoverflow.com/questions/147659/get-list-of-databases-from-sql-server. Would you mind to add connection string? – IvanH May 10 '13 at 07:12
  • 1
    You need to check your connection string, but I am pretty sure that this query will not work. The GO isn't a word that has any meaning here. – Steve May 10 '13 at 07:34
  • 1
    @Steve: you are correct http://stackoverflow.com/questions/971177/using-go-within-a-transaction/971199#971199 – gbn May 10 '13 at 08:05
  • Thanks for you time to comment all.. I was very sure that the database in there. Actually, in the folder have 2Database. I can access Notif_DB but i can't access VIRTUALBOOK. Also, the first connection made is to notif_db, after few days, i made new connection to virtualbook. And today i test it. It pop up that error. I don't think it is database missing – Kasnady May 10 '13 at 08:22
  • But what is the connection string used in the vbCon variable? This is a fundamental information to know. And where do you get the exception? On the Open statement or on the ExecuteNonQuery? – Steve May 10 '13 at 08:48
  • vbCon = SqlVB(above the picture), SQLVB = SQLPLACE(just the database name change only).. Actually i don't know where the exception error at. Cause i was using TRY.. And i have found out the answer – Kasnady May 13 '13 at 02:50

1 Answers1

0

I was follow on @IvanH , @Steve, @gbn. To remove GO.. Actually first i got error too. But then i remove the USE statments.

And i remove the cmd.commandtype statements too. Now it's run, i have test to INSERT and SELECT the database Table and it show well.. Below is my code. If there are any bad logic of my code. Please give me some advice, cause i still a beginner for this. Thanks for your time all..

 Dim sqlcon2 As New SqlConnection(My.Settings.SqlVB)
        MainMenu.lblStatus.Text = "Creating Virtual Book"
        sqlstr2 = "CREATE TABLE [dbo].[" & tbEmail.Text & "](" & _
                      "[ID] [ntext] NULL, " & _
    "[REMARKS] [NCHAR](6) NULL " & _
") ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]"
        Try
            sqlcon2.Open()
            cmd = New SqlCommand(sqlstr2, sqlcon2)

            cmd.ExecuteNonQuery()
            sqlcon2.Close()
            MainMenu.lblStatus.Text = "Command Saved to Server"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

And i change the connection directly to My.Settings.SqlVB. Just can see the string at the Picture above(my question)

Kasnady
  • 2,249
  • 3
  • 25
  • 34