简体   繁体   English

使用参数的 Oledb Insert 语句

[英]Oledb Insert statement using parameters

How do I insert into parameters from different tables using OleDB?如何使用 OleDB 从不同表插入参数?

I have 3 tables: 1. itemTbl 2. crateTbl 3. contentTbl我有 3 个表:1. itemTbl 2. crateTbl 3. contentTbl

itemTbl has: itemID, itemName, itemDesc crateTbl has: crateID, crateName contentTbl has: crateID, itemID, qty itemTbl 有:itemID、itemName、itemDesc crateTbl 有:crateID、crateName contentTbl 有:crateID、itemID、qty

contentTbl is the contents of the crate and the qty of each contentTbl 是板条箱的内容和每个板条箱的数量

I need it to select values in different tables I use WHERE .我需要它来选择我使用的不同表中的值WHERE I have tried a similar code using a local db and service based db and they allow me, but OleDB doesn't let me use VALUES((SELECT))....我已经使用本地数据库和基于服务的数据库尝试了类似的代码,他们允许我使用,但 OleDB 不允许我使用 VALUES((SELECT))....

Error message:错误信息:

System.Data.OleDb.OleDbException: 'Query input must contain at least one table or query. System.Data.OleDb.OleDbException: '查询输入必须至少包含一个表或查询。

My code:我的代码:

cmd.Dispose();

cmd.CommandText = @"INSERT INTO contentTbl(crateID,itemID,qty) VALUES((SELECT crateTbl.crateID FROM crateTbl WHERE crateTbl.crateID=?),(SELECT itemTbl.itemID FROM itemTbl WHERE itemTbl.itemID = ?), ?)";
cmd.Connection = con;

cmd.Parameters.Add(new OleDbParameter("crateID", txtCrate.Text));
cmd.Parameters.Add(new OleDbParameter("itemID", txtItem.Text));
cmd.Parameters.Add(new OleDbParameter("qty", txtQty.Text));

con.Open();
cmd.ExecuteNonQuery();
con.Close();

MessageBox.Show("Done!");

da.SelectCommand = new OleDbCommand("SELECT * FROM contentTbl", con);
da.Fill(dt);

dgvContent.DataSource = dt;

The error message is quite descriptive.错误消息非常具有描述性。 Access doesn't support subqueries without a main query, so change the syntax round to use one of the subqueries as your main query: Access 不支持没有主查询的子查询,因此更改语法回合以使用子查询之一作为主查询:

INSERT INTO  contentTbl(crateID,itemID,qty)
SELECT crateTbl.crateID,(SELECT itemTbl.itemID FROM itemTbl WHERE itemTbl.itemID = ?), ?
FROM crateTbl WHERE crateTbl.crateID=?

Note that parameters are passed by position, and rewriting this query does require you to re-order parameters:请注意,参数是按位置传递的,重写此查询确实需要您重新排序参数:

cmd.Parameters.Add(new OleDbParameter("itemID", txtItem.Text));
cmd.Parameters.Add(new OleDbParameter("qty", txtQty.Text));
cmd.Parameters.Add(new OleDbParameter("crateID", txtCrate.Text));

If you don't like the main query/subquery syntax, you can go for a cross join too:如果您不喜欢主查询/子查询语法,您也可以进行交叉联接:

INSERT INTO  contentTbl(crateID,itemID,qty)
SELECT crateTbl.crateID, itemTbl.itemID, ?
FROM crateTbl,itemTbl  
WHERE crateTbl.crateID=? AND itemTbl.itemID = ?

(Parameter order needs to be adjusted again but you can figure that out). (参数顺序需要再次调整,但你可以弄清楚)。

I am not sure about the syntax.我不确定语法。 I always use it like (this is from VB, but C# should be similar) :我总是像这样使用它(这是来自 VB,但 C# 应该类似):

cmd.Parameters.Addwithvalue("crateID", txtCrate.Text) cmd.Parameters.Addwithvalue("crateID", txtCrate.Text)

I have tried this and it sort of works, but for some reason works only with values for the items table as ID's.我试过这个,它有点工作,但由于某种原因,只适用于作为 ID 的项目表的值。 If I have an ID (they're ints) that crateTbl has but itemTbl doesn't, it doesn't insert.如果我有一个 crateTbl 有但 itemTbl 没有的 ID(它们是整数),它不会插入。

cmd.Dispose();
cmd.CommandText = @"INSERT INTO contentTbl(itemID,crateID,qty) SELECT itemTbl.itemID,(SELECT crateTbl.crateID FROM crateTbl WHERE crateTbl.crateID = ?), ? FROM itemTbl WHERE itemTbl.itemID=?";

cmd.Connection = con;

cmd.Parameters.Add(new OleDbParameter("itemID", txtItem.Text));
cmd.Parameters.Add(new OleDbParameter("qty", txtQty.Text));
cmd.Parameters.Add(new OleDbParameter("crateID", txtCrate.Text));

con.Open();
cmd.ExecuteNonQuery();
con.Close();

MessageBox.Show("Done!");

dt2.Clear();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM