[英]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.