【发布时间】:2018-06-25 04:13:52
【问题描述】:
OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\Computing\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Database31.accdb");
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT Cocktails.CID, Ingredients.Ingredient1" +
" FROM Cocktails INNER JOIN Ingredients" +
" ON Cocktails.ID = Ingredients.ID" +
" WHERE Cocktails.ID = 1",
connection);
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read())
{
string result = reader.GetValue(0).ToString();
string result2 = reader.GetValue(1).ToString();
MessageBox.Show(result + result2);
}
connection.Close();
我是 SQL 新手,我尝试使用“内部连接”从两个不同的表中提取信息:“连接操作中的语法错误。” 是我得到的错误并且不确定为什么。谢谢
【问题讨论】:
-
Ingredient.ID和Cocktail.ID是什么?不应该是Ingredients.ID和Cocktails.ID吗? -
您在查询中有
Cocktail和Cocktails。选择正确的。 (即使你做对了,我怀疑JOIN是否真的在id列上。) -
您的表是叫
Cocktails和Ingredients还是叫Cocktail和Ingredient?查询似乎无法决定哪个。 -
澄清@GordonLinoff 所说的:检查您是否也有
CocktailIngredients表。如果存在,那么您需要在加入时将该表用作“中间人”。鸡尾酒与成分等于似乎不太可能,但这就是您通过将它们直接链接到它们的两个 ID 列来暗示的意思。 -
嗨,是的,他们错了,但现在我收到错误消息“没有为一个或多个必需参数提供值。”
标签: c# sql database inner-join oledb