简体   繁体   English

Visual C#SQL Server罗斯文数据库错误:无效的对象名称'dbo.Products'

[英]Visual C# SQL Server Northwind Database Error: Invalid Object Name 'dbo.Products'

My issue is that I keep getting an error on console that says 我的问题是我在控制台上不断收到错误消息,说

Invalid Object Name 'dbo.Products' 无效的对象名称“ dbo.Products”

I am using Visual C# 2008 Express Edition and SQL Server 2008 Express. 我正在使用Visual C#2008 Express Edition和SQL Server 2008 Express。

I have had some issue with preparing/installing the Northwind sample database, so I'm unsure if that has any weight on the issue though. 我在准备/安装Northwind示例数据库时遇到了一些问题,因此我不确定这对问题是否有任何影响。 Our teacher gave a test program which is the program I am receiving this error. 我们的老师给了一个测试程序,这是我收到此错误的程序。

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();
    }
}

Your database is not Northwinds but Pubs . 您的数据库不是Northwinds而是Pubs

Check the Connection String 检查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";

Specifically on AttachDBFileName : 特别是在AttachDBFileName

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

Try to change it to Northwinds (given it is in the same folder as Pubs), like: 尝试将其更改为Northwinds(假设它与Pubs在同一文件夹中),例如:

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

Update : 更新

Try to use SqlCeConnection instead of Sqlconnection because it looks like you are using compact or SDF : 尝试使用SqlCeConnection而不是Sqlconnection因为看起来您正在使用compact或SDF

 using (SqlConnection connection =
    new SqlConnection(connectionString))

To: 至:

 using (SqlCeConnection connection =
    new SqlCeConnection(connectionString))

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

相关问题 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 如何在C#中的SQL视图中使用ToList? 带有无效对象名称dbo的疼痛。* - How do you use ToList with an SQL view in C#? Pains with Invalid object name dbo.* C# - 实体框架错误:对象名称“dbo.TableName”无效 - C# - Entity Framework error: invalid object name 'dbo.TableName' Sql 数据库 System.Data.SqlClient.SqlException: '无效的对象名称 'dbo.LoginTbl'。 - Sql Database System.Data.SqlClient.SqlException: 'Invalid object name 'dbo.LoginTbl'.' SQL Server数据库中的对象名称无效? - Invalid object name from SQL Server database? SQL更新:无效的对象名称'master.dbo.TsqlSplit' - SQL Update: Invalid object name 'master.dbo.TsqlSplit' 无效的对象dbo.UserShoppingItems-Azure IIS和SQL Server - Invalid object dbo.UserShoppingItems - Azure IIS & SQL server C# LINQ to SQL 无效的对象名称 - C# LINQ to SQL Invalid Object Name 我的控制器中的无效对象名称“ dbo.MapLocations”错误 - Invalid object name 'dbo.MapLocations' error in my controller 使用northwind DB对c#进行sql查询 - sql query to c# using northwind DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM