[英]where condition in MySQL Insert statement
Can I use where condition in Insert statement???? 我可以在Insert语句中使用where where ???? I have coded like this, its showng me an error call MySQLException was unhandled, You have an error in your SQL syntax;
我编写了这样的代码,它显示错误调用MySQLException未处理,你的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE RegistrationID='3'' at line 1. My code:-
检查与MySQL服务器版本对应的手册,以便在第1行的'WHERE RegistrationID ='3''附近使用正确的语法。我的代码: -
MySqlCommand cmd1 = new MySqlCommand("INSERT INTO registration(DueAmount) VALUES ('"+textBox5.Text + "') WHERE RegistrationID='"+textBox2.Text+"'",connection);
The correct syntax of INSERT
doesn't have WHERE
clause. INSERT
的正确语法没有WHERE
子句。 I think you want UPDATE
instead of INSERT
, 我想你想要
UPDATE
而不是INSERT
,
UPDATE registration
SET DueAmount = 'txt5'
WHERE RegistrationID = 'txt2'
the only way you can use WHERE
in SELECT
is when you are using INSERT INTO....SELECT
statement. 在
SELECT
使用WHERE
的唯一方法是使用INSERT INTO....SELECT
语句。
one more thing, since you are using ADO.NET
, make sure that you parameterized your query to avoid SQL Injection
, and use USING
statement. 还有一件事,因为您使用的是
ADO.NET
,请确保参数化您的查询以避免SQL Injection
,并使用USING
语句。
string query = "UPDATE registration
SET DueAmount = @dateAmount
WHERE RegistrationID = @RegID"
using (MySqlCommand cmd1 = new MySqlCommand(query,connection))
{
cmd1.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@dateAmount", textBox5.Text);
cmd.Parameters.AddWithValue("@RegID", textBox2.Text);
// other codes
}
You're mixing 2 different statements. 你混合了2个不同的陈述。
UPDATE
statement updates an existing row in your table. UPDATE
语句更新表中的现有行。 INSERT
statement adds a new row in your table. INSERT
语句在表中添加一个新行。 I think you want to use an UPDATE
statement and modify an existing row. 我想你想使用
UPDATE
语句并修改现有的行。
MySqlCommand cmd1 = new MySqlCommand("
UPDATE Registration Set DueAmount= '"+textBox5.Text
+ "' WHERE RegistrationID='"+textBox2.Text+"'",connection);
INSERT
with WHERE
doesn't make sense. 使用
WHERE
INSERT
没有意义。 INSERT
always inserts a new row. INSERT
始终插入新行。 You might be looking for REPLACE INTO
which does a insert if that record doesnt exist or an update if it does based on its primary key. 您可能正在寻找
REPLACE INTO
,如果该记录不存在则执行插入,如果基于其主键执行更新,则执行更新。
INSERT
puts a new line to database. INSERT
为数据库添加一个新行。 You can not put a new line WHERE
sth is sth. 你不能把一个新行
WHERE
某事某物是。 But you can UPDATE
it. 但你可以
UPDATE
它。 Hope this helps. 希望这可以帮助。
You need to use an UPDATE statement. 您需要使用UPDATE语句。
tHS SYNTAX IS SIMILAR: "UPDATE registration SET DueAmount = '" + textBox5.Text + "' WHERE RegistrationID='"+textBox2.Text+"'" tHS语法类似:“UPDATE registration SET DueAmount ='”+ textBox5.Text +“'WHERE RegistrationID ='”+ textBox2.Text +“'”
You can try with Update
您可以尝试使用
Update
var query = "UPDATE Registration SET DueAmount= $Paremeter1 WHERE RegistrationID = $Paremeter2";
var cmd1 = new MySqlCommand(query, connection);
cmd1 .Parameters.AddWithValue("$Paremeter1", textBox5.Text);
cmd1 .Parameters.AddWithValue("$Paremeter2", textBox2.Text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.