本文介绍了查询输入必须至少包含一个表或查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子:

Table1
$
PrimaryKey

名称

Table1
PrimaryKey
Name

表2

PrimaryKey

ForeignKey

BackColor

Table2
PrimaryKey
ForeignKey
BackColor

我正在尝试插入主键表1到表2作为外键执行此操作:

I'm trying to insert Primary key from Table1 to Table2 as a Foreign key by doing this:

cmd = new OleDbCommand("INSERT INTO [Table2] (ForeignKey, BackColor) VALUES ((SELECT [ID] FROM [Table1] WHERE [NAME] = @name), @backColor)", connection);
                    cmd.Parameters.AddWithValue("@name", name);
                    cmd.Parameters.AddWithValue("@backColor", backColor);
                    cmd.ExecuteNonQuery();           

然后我收到错误:

Then I get the error:

"查询输入必须包含至少一个表格或查询。" 

"Query input must contain at least one table or query." 

可能出现什么问题?

推荐答案

StringBuilder query = new StringBuilder();
query.AppendLine("INSERT INTO [Table2] (ForeignKey, BackColor)");
query.AppendLine("SELECT [ID], @backColor FROM [Table1] WHERE [NAME] = @name");
cmd = new OleDbCommand(query.ToString(), connection);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@backColor", backColor);
cmd.ExecuteNonQuery();   


这篇关于查询输入必须至少包含一个表或查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:12