创建表未显示 [英] Create Table not showing up

查看:64
本文介绍了创建表未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用以下代码在SQL Server 2008 R2上的数据库中创建表。该表未显示在VS2013或SQL Studio中数据库/表下的数据连接中。但是,方法DbTableExists说它确实存在。我曾尝试使用和不使用[dbo]。[table-name],没有什么区别。



我尝试刷新,关闭/重启SQL Studio,我仍然无法显示。



非常感谢任何帮助。



谢谢,

格伦



<前lang =c#> 如果(!DbTableExists(connectionString, [dbo]。[Server]))
{
尝试
{
使用(SqlConnection connection = < span class =code-keyword> new SqlConnection(connectionString))
{
string strCreateTable =
< span class =code-string> @ CREATE TABLE [dbo]。[Server]
(ServerName nvarchar(15)PRIMARY KEY,
La stUpdate datetime,
;

SqlCommand command = new SqlCommand(strCreateTable,connection);
command.CommandType = CommandType.Text;
connection.Open();
command.ExecuteReader();
connection.Close();
}
}
...

私人 static bool DbTableExists( string strConnection, string strTableNameAndSchema)
{
使用(SqlConnection connection = new SqlConnection (strConnection))
{
string strCheckTable =
String 。格式( IF OBJECT_ID('{0}','U')IS NOT NULL SELECT'true'ELSE SELECT' false'
strTableNameAndSchema);

SqlCommand command = new SqlCommand(strCheckTable,connection);
command.CommandType = CommandType.Text;
connection.Open();

return Convert.ToBoolean(command.ExecuteScalar());
}
}

解决方案

< br /> 
string strCreateTable =< br />
@CREATE TABLE [dbo]。[Server]< br />
(ServerName nvarchar(15)PRIMARY KEY,< br />
LastUpdate datetime,< br />
);< br />



尝试删除 dbo 并再次运行查询。


好的,我只能说哇。我很惊讶没有抛出异常。问题是我在创建数据库以包含数据库名称后忘记更新连接字符串。正在创建这些表,可能是在一个系统数据库下。


Hi, I'm using the following code to create a table within a database on SQL Server 2008 R2. The table doesn't show up in the "Data Connection" under the database/table in VS2013 or SQL Studio. However, the method DbTableExists says it does exist. I've tried with and without the [dbo].[table-name], doesn't make a difference.

I've tried refreshing, closing/restarting SQL Studio, and I still can't it to show up.

Any help is greatly appreciated.

Thanks,
Glenn

if (!DbTableExists(connectionString, "[dbo].[Server]"))
{
    try
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            string strCreateTable =
                @"CREATE TABLE [dbo].[Server] 
                (ServerName	    nvarchar(15) PRIMARY KEY,
                 LastUpdate         datetime,
                )";

            SqlCommand command = new SqlCommand(strCreateTable, connection);
            command.CommandType = CommandType.Text;
            connection.Open();
            command.ExecuteReader();
            connection.Close();
       }
   }
...

private static bool DbTableExists(string strConnection, string strTableNameAndSchema)
{
    using (SqlConnection connection = new SqlConnection(strConnection))
    {
        string strCheckTable = 
           String.Format("IF OBJECT_ID('{0}', 'U') IS NOT NULL SELECT 'true' ELSE SELECT                        'false'",
                      strTableNameAndSchema);

        SqlCommand command = new SqlCommand(strCheckTable, connection);
        command.CommandType = CommandType.Text;
        connection.Open();

        return Convert.ToBoolean(command.ExecuteScalar());
    }
}

解决方案

<br />
            string strCreateTable =<br />
                @"CREATE TABLE [dbo].[Server] <br />
                (ServerName	    nvarchar(15) PRIMARY KEY,<br />
                 LastUpdate         datetime,<br />
                )";<br />


Try removing dbo and run the query again.


OK, all I can say is wow. I'm surprised there wasn't an exception thrown. The problem was I forgot to update the connection string after I created the database to include the database name. The tables were being created, probably under one of the System Database.


这篇关于创建表未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆