2014-07-06 27 views
0

在我的Windows 7 Pro上運行的Visual Basic 2013 ASP.NET Web應用程序項目中,出現此錯誤每次我運行它。獲取SQL Server錯誤:「數據庫中已有一個名爲'<my table>'的對象。」當表不在數據庫中時

如果在運行之前刪除我的表,我的應用程序會創建表,但我會收到錯誤消息。

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

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 
+0

你能發表ExecuteSQLStmt的代碼嗎?有些東西不清楚,爲什麼你輸入if條件,如果該表也存在於MASTER數據庫中 – Steve

回答

0

的問題是,我用一個GridView UI控件來看看錶,但在GridView沒有被更新,所以從來不表明桌子已經在那裏了。

通過更新GridView解決了問題。

3

您的連接字符串未指定目錄名稱(數據庫名稱),因此您的查詢是針對「MASTER」數據庫執行的。如果你看一下有可能你會發現,你認爲不存在表

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

實際上,它將針對可能或不可能是'master'的登錄的缺省數據庫執行。 –

+0

是的,你當然是對的,但是在OP代碼中仍然不清楚。我想第一個ExecuteSQLStmt使用相同的連接,所以它應該是成功的。 – Steve

+0

對不起。我只是添加了缺失:Function ExecuteSQLStmt()。其中有正確的數據庫名稱。新聞:我得到它的工作。錯誤指出了一些與GridView有關的事情,所以我打開了GridView的屬性,並通過發送'select * from different database''然後彈出一些關於數據庫刷新的消息。我再次嘗試,它的工作。 –

相關問題