问题
I'm trying to replicate northwind
database but when I created parameter query vw_CustomerOrderYear
, the query is not showing in my datasource.
I do not know what I'm missing..
My parameter query vw_CustomerOrderYear
is :
PARAMETERS OrderYear Long;
SELECT DISTINCT Customer.ID, Customer.Name, Customer.City, Customer.Country,
Year([Order].OrderDate) AS OrderYear
FROM Customer INNER JOIN [Order] ON Customer.ID=Order.CustomerId
WHERE (((Year(Order.OrderDate))=[OrderYear])) Or ((([OrderYear]) Is Null));
How can I get this query under my SomeNameDataSet
?
回答1:
When you create a "parameter query" in the Microsoft Access application itself (MSACCESS.EXE), i.e., one whose SQL code looks like ...
PARAMETERS ... ; SELECT ... FROM ...
... it is considered a "Query" by ACE/Jet DAO (Data Access Objects), which is the technology that MSACCESS uses to communicate with the Access Database Engine (ACE or Jet). You can run the query in Access and get prompted for the parameter values, or you can use a DAO QueryDef object in VBA and pass the parameter values to the .Parameters collection of that QueryDef object.
"Dataset" Data Sources in .NET projects behave differently because they use System.Data.OleDb, and the ACE/Jet OLEDB provider does not classify those queries as "Views". Instead, it classifies them as "[Stored] Procedures". So, if you were to open an OleDbConnection and do
DataTable dtViews = con.GetSchema("Views")
then the query would not show up in the resulting list. That's why you cannot see it under "Views" in the Data Sources wizard in Visual Studio.
However, it does show up if you do
DataTable dtProcedures = con.GetSchema("Procedures")
So, you can use the existing Access "parameter query" by treating it like a Stored Procedure in your .NET code, e.g.,
using (var cmd = new OleDbCommand())
{
cmd.Connection = con; // an open OleDbConnection to the database
cmd.CommandText = "vw_CustomerOrderYear";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("OrderYear", OleDbType.Integer).Value = 2014;
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
}
来源:https://stackoverflow.com/questions/27916036/created-parameterized-query-not-showing-in-datasource-visual-studio-10-and-acces