[英]Getting SQL Server error: “There is already an object named '<my table>' in the database. ” when table is NOT in database
In my Visual Basic 2013 ASP.NET web application project running on Windows 7 Pro, I get this error every time I run it. 在Windows 7 Pro上运行的Visual Basic 2013 ASP.NET Web应用程序项目中,每次运行它都会收到此错误。
If I drop my table before running it, my app creates table, but I get error. 如果我在运行表之前将其删除,则我的应用会创建表,但会出现错误。
If table exists before running it, app tries to create table, ignores error and continues, but I get error. 如果表在运行之前存在,则应用会尝试创建表,忽略错误并继续,但是出现错误。
The Page_Load
function steps through as expected, without error, to the end. Page_Load
函数按预期Page_Load
进行,没有错误,直到最后。
And then the error occurs. 然后发生错误。
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
Your connection string doesn't specify the catalog name (the database name) so your query is executed against the "MASTER" database. 您的连接字符串未指定目录名称(数据库名称),因此对“ MASTER”数据库执行查询。 If you look there probably you will find the table that you think is not present
如果您看那里,可能会发现您认为不存在的表
Private ConnectionString As String = @"Integrated Security=SSPI;" & _
"Initial Catalog=.....database name here;" & _
"Data Source=localhost;"
The problem was that I was using a GridView UI control to look at the table, but the GridView was not being updated, so never showed that table was already there. 问题是我正在使用GridView UI控件查看表,但是GridView没有被更新,所以从不显示该表已经存在。
Problem solved by updating GridView. 通过更新GridView解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.