简体   繁体   English

.NET,C#,SQL Server:带有CREATE TABLE语句的无效对象名称

[英].NET, C#, SQL Server: Invalid object name with CREATE TABLE statement

I am trying to execute a CREATE TABLE which results in the following SQL exception: 我正在尝试执行一个CREATE TABLE,导致以下SQL异常:

Invalid object name 'UserLock'. 无效的对象名称“ UserLock”。

The statement looks like this: 该语句如下所示:

USE [db]
GO

CREATE TABLE [db].[dbo].[UserLock] (
    [Login] [varchar](150) NOT NULL,
    [ExpirationDate] [datetime] NOT NULL,
    CONSTRAINT [PK_UserLock] PRIMARY KEY CLUSTERED
    ([Login] ASC)
    WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

The strange part is that I can run the query successfully inside the Microsoft SQL Management Studio with the same user but not within my .NET web application written in C#. 奇怪的是,我可以用相同的用户在Microsoft SQL Management Studio中成功运行查询,但不能在用C#编写的.NET Web应用程序中运行查询。 I am not using any frameworks and I connect to the database with the provided classes out of System.Data.SqlClient. 我没有使用任何框架,而是使用System.Data.SqlClient中提供的类连接到数据库。 All other database queries work within the app. 所有其他数据库查询均在该应用程序内运行。 The database is Microsoft SQL Express 2005. 该数据库是Microsoft SQL Express 2005。

-- Edit --- -编辑-

This is how my execution code looks like: 这是我的执行代码如下所示:

    string createString = "CREATE TABLE [" + catalog + "].[dbo].[UserLock]("
                        + "  [Login] [varchar](150) NOT NULL,"
                        + "  [ExpirationDate] [datetime] NOT NULL,"
                        + "  CONSTRAINT [PK_UserLock] PRIMARY KEY CLUSTERED "
                        + "  ([Login] ASC)"
                        + "  WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"
                        + ") ON [PRIMARY]";

    SqlCommand createCommand = connection.CreateCommand();
    createCommand.Connection = connection;
    createCommand.CommandText = createString;

    selectCommand.ExecuteNonQuery();

I catch the exception in another method. 我用另一种方法捕获异常。 The SQL connection itself is beeing set up in antoher method, aswell. SQL连接本身也可以通过其他方法进行设置。 It's the standard SqlConnection connection = new SqlConnection(connectionString); 这是标准的SqlConnection connection = new SqlConnection(connectionString);

I am not sure how you are executing the above SQL but be aware that GO is not a SQL keyword, it is a command to tell SQL Management Studio to execute the current batch. 我不确定您如何执行上述SQL,但要知道GO并不是SQL关键字,它是一个告诉SQL Management Studio执行当前批处理的命令。 IE you cannot use GO outside of SQL management studio. IE,您不能在SQL Management Studio之外使用GO。 Also i can't see how using GO would result in the above error. 我也看不到如何使用GO会导致上述错误。 It might be best if you post the entire c# code that fails as the error might be elsewhere. 最好是发布失败的整个c#代码,因为该错误可能在其他地方。

You can completely remove the following code: 您可以完全删除以下代码:

USE [db] GO

You have fully qualified the create table statement with [Database].[Schema].[ObjectName] so it will create the table in the correct database and schema anyway. 您已经使用[Database]。[Schema]。[ObjectName]完全限定了create table语句,因此无论如何它将在正确的数据库和模式中创建表。

Also, you shouldn't use GO in C# as it is specific to the SQL Mgmt Studio and is used to separate batches of SQL statements. 另外,您不应该在C#中使用GO ,因为它特定于SQL Mgmt Studio,并且用于分隔成批SQL语句。 If you really must use GO then take a look at Sql Server Management Object classes. 如果您确实必须使用GO那么请看一下Sql Server管理对象类。

There is a nice blog post from Jon Galloway about it. 乔恩·加洛韦(Jon Galloway)有一篇不错的博客文章

If you rename the table name? 如果重命名表名? Does it work then, could be a protected word. 那它有用吗,可能是一个受保护的词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中表名称的无效对象名称SQL异常 - invalid object name sql exception for table name in c# 无法在C#中动态创建SQL表-“无效的列名” - Unable to create SQL Table dynamically in c# — “invalid column name” 从C#中的SQL Create语句检索表名 - Retrieve table name from SQL Create statement in C# 列名无效 C# SQL 语句 - Invalid column name C# SQL statement 在SQL Server中创建一个表,其中的名称是C#中的变量? - Create a table in SQL Server where the name is a variable in C#? C# LINQ to SQL 无效的对象名称 - C# LINQ to SQL Invalid Object Name 如何在C#中解析无效的对象名“表名”? - How to resolve Invalid Object Name “Table Name” in C#? SqlException: Invalid object name ERROR WHILE BUILDING WEBSITE IN C# (VISUAL STUDIO) 和 sql 服务器 - SqlException: Invalid object name ERROR WHILE BUILDING WEBSITE IN C# (VISUAL STUDIO) and sql server Visual C#SQL Server罗斯文数据库错误:无效的对象名称'dbo.Products' - Visual C# SQL Server Northwind Database Error: Invalid Object Name 'dbo.Products' 带有 SQL Server 数据库的 .NET Core 应用程序,其中表名以 C# 为前缀 - 动态表名 - .NET Core application with SQL Server database where table names are prefixed C# - dynamic table name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM