1

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

无效的对象名称“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();
    }
}
4

1 回答 1

0

您的数据库不是Northwindsbut 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))
于 2014-09-17T22:57:52.913 回答