簡體   English   中英

出現SQL Server錯誤:“已經有一個名為&#39; <my table> 在數據庫中。 “表不在數據庫中時”

[英]Getting SQL Server error: “There is already an object named '<my table>' in the database. ” when table is NOT in database

在Windows 7 Pro上運行的Visual Basic 2013 ASP.NET Web應用程序項目中,每次運行它都會收到此錯誤。

如果我在運行表之前將其刪除,則我的應用會創建表,但會出現錯誤。

如果表在運行之前存在,則應用會嘗試創建表,忽略錯誤並繼續,但是出現錯誤。

Page_Load函數按預期Page_Load進行,沒有錯誤,直到最后。

然后發生錯誤。

Imports System.Data.SqlClient

Public Class WebForm2

    Inherits System.Web.UI.Page


    Private ConnectionString As String = "Integrated Security=SSPI;" + "Initial Catalog=;" + "Data Source=localhost;"
    Private reader As SqlDataReader = Nothing
    Private conn As SqlConnection = Nothing
    Private cmd As SqlCommand = Nothing
    Private sql As String = Nothing

    Public Const DEBUG_DEFAULT_CLIENT_ID = "NATIONS_BURGERS"
    Public Const DEBUG_DEFAULT_JOB_ID = "FRY_COOK_2014_07_05"
    Public client_ID
    Public job_ID
    Public client_job_table

    ' InitializeS the job application page. 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' Initialize data: 
        client_ID = DEBUG_DEFAULT_CLIENT_ID
        job_ID = DEBUG_DEFAULT_JOB_ID
        client_job_table = client_ID + "_" + job_ID

        ' App selects client job table : 
        Dim command_successful =
        ExecuteSQLStmt(" select * from " + client_job_table)
        ' Create table if it doesn't exist:
        If Not command_successful Then
            ExecuteSQLStmt("CREATE TABLE " + client_job_table + _
                        "(" + _
                        "FIRST_NAME varchar(255)," + _
                        "LAST_NAME varchar(255)," + _
                        "PHONE varchar(255)" + _
                        ");")

        End If

        set_22_button.Visible = GridView1.Visible
        set_333_button.Visible = GridView1.Visible
    End Sub


' Sends sql command to ehires database.
Private Function ExecuteSQLStmt(ByVal sql As String)
    ' Open the connection

    ConnectionString = "Data Source=<my IP>;Initial Catalog=<my database name>;Persist Security Info=True;User ID=Doug;Password=ThankYou!!"
    Dim connection As New SqlConnection(ConnectionString)

    connection.ConnectionString = ConnectionString
    connection.Open()
    cmd = New SqlCommand(sql, connection)
    Dim command_successful = True
    On Error Resume Next
    cmd.ExecuteNonQuery()
    If Err.Number Then
        command_successful = False
        Dim reason = Err.Description
    Else
        command_successful = True
    End If
    connection.Close()
    Return command_successful
End Function  'ExecuteSQLStmt 

您的連接字符串未指定目錄名稱(數據庫名稱),因此對“ MASTER”數據庫執行查詢。 如果您看那里,可能會發現您認為不存在的表

Private ConnectionString As String = @"Integrated Security=SSPI;" & _
                                      "Initial Catalog=.....database name here;" & _
                                      "Data Source=localhost;"

問題是我正在使用GridView UI控件查看表,但是GridView沒有被更新,所以從不顯示該表已經存在。

通過更新GridView解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM