数据库不存在,但它存在于那里

时间:2013-05-10 04:21:35

标签: vb.net sql-server-2012

在我的项目中有2Databases,而我的服务器正在使用SQL Server 2012 Management Studio。现在我想连接到其中一个数据库并在运行时创建一个新表。 但我遇到了问题。

Database Not Exist

但是当我检查我的位置时。它存在且具有相同的名称..

这是我的代码: 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

1 个答案:

答案 0 :(得分:0)

我跟随@IvanH,@ Steve,@ gbn。删除GO ..实际上我首先得到了错误。但是我删除了USE声明。

我也删除了cmd.commandtype语句。现在它运行了,我测试INSERT和SELECT数据库表,它显示良好..下面是我的代码。如果我的代码有任何错误的逻辑。请给我一些建议,因为我还是初学者。谢谢你的时间......

 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

我将连接直接更改为My.Settings.SqlVB。只是可以看到上图中的字符串(我的问题)