【问题标题】:Visual C# SQL Server Northwind Database Error: Invalid Object Name 'dbo.Products'Visual C# SQL Server Northwind 数据库错误:无效的对象名称“dbo.Products”
【发布时间】:2014-09-17 22:54:29
【问题描述】:

我的问题是我在控制台上不断收到错误提示

无效的对象名称“dbo.Products”

我正在使用 Visual C# 2008 Express Edition 和 SQL Server 2008 Express。

我在准备/安装Northwind 示例数据库时遇到了一些问题,所以我不确定这是否对这个问题有任何影响。我们的老师给出了一个测试程序,这是我收到此错误的程序。

static void Main()
{
    string connectionString =
            "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

    string queryString =
        "SELECT ProductID, UnitPrice, ProductName FROM dbo.Products "
            + "WHERE UnitPrice > @pricePoint "
            + "ORDER BY UnitPrice DESC;";

    int paramValue = 5;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Parameters.AddWithValue("@pricePoint", paramValue);

        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}\t{2}",
                    reader[0], reader[1], reader[2]);
            }

            reader.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadLine();
    }
}

【问题讨论】:

    标签: c# database visual-studio sql-server-2008 invalid-object-name


    【解决方案1】:

    您的数据库不是Northwinds,而是Pubs

    查看Connection String

     string connectionString =
            "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
    

    特别是AttachDBFileName:

    AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\PUBS.MDF
    

    尝试将其更改为 Northwinds(假设它与 Pubs 位于同一文件夹中),例如:

    AttachDbFilename=C:\\Users\\Andrew\\Desktop\\SQL Server  2000 Sample Databases\\Northwind.MDF
    

    更新

    尝试使用SqlCeConnection 而不是Sqlconnection,因为看起来您使用的是紧凑型或SDF

     using (SqlConnection connection =
        new SqlConnection(connectionString))
    

    收件人:

     using (SqlCeConnection connection =
        new SqlCeConnection(connectionString))
    

    【讨论】:

    • 在文件夹中我有“Notherwind.sdf”、“NORTHWND.LDF”、“PUBS.MDF”、“PUBS_LOG.LDF”、“instnwnd.sql”、“instpubs.sql”和一个自述文本文件。默认的代码确实是Northwind.MDF,但我没有。现在得到的错误是“尝试为文件 C:\path\to\file\Notherwind.SDF 附加自动命名的数据库失败。存在同名的数据库,或者无法打开指定的文件,或者它是位于 UNC 共享"。
    • 看起来您使用的是 sql 的精简版,而不是常规的 SQL expre3ss 服务器。检查我上面的更新。
    • 很抱歉给您提供了错误的信息。但我已经解决了关于“NORTHWND.MDF”的问题。不幸的是,我有一个新错误,在我粘贴之前请记住。我从 SQL Server 3.5 示例文件夹中复制并粘贴了 Northwind.sdf 文件,我认为这是导致此错误的原因。
    • 错误:一个或多个文件与数据库的主文件不匹配。如果您尝试附加数据库,请使用正确的文件重试该操作。如果这是现有数据库,则文件可能已损坏,应从备份中恢复。无法打开用户默认数据库。登录失败。用户 'LENOVO-PC\Andrew' 登录失败。日志文件“C:\SQL Server 2000 Sample Databases\NORTHWND_log.ldf”与主文件不匹配。它可能来自不同的数据库,也可能之前已重建日志。
    • 花你的男人。只是为了向正在阅读本文的任何人解释,同时修复 .MDF 和路径问题,我重新安装了示例数据库。当您卸载它时,它会保留原始文件夹中的日志文件......删除它。砰它的作品非常感谢老兄。我非常感谢。感谢您成为我第一个加入这个很棒的社区的人。
    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多