C# 纠正语法
我必须更新一个表,我尝试了两个查询,但它显示语法错误,任何人都可以帮助我,请我知道这是基本的,但我是一个乞丐C# 纠正语法,c#,sql,ms-access,C#,Sql,Ms Access,我必须更新一个表,我尝试了两个查询,但它显示语法错误,任何人都可以帮助我,请我知道这是基本的,但我是一个乞丐 String query = "Update masterusertable set username='" + txtName.Text + "',and set password='" + txtpassword.Text + "',and set phoneno='" + txtphoneno.Text + "',and set adress='" + rtxtAdress.Tex
String query = "Update masterusertable set username='" + txtName.Text + "',and set password='" + txtpassword.Text + "',and set phoneno='" + txtphoneno.Text + "',and set adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'";
String query = "Update masterusertable set username='" + txtName.Text + "', password='" + txtpassword.Text + "', phoneno='" + txtphoneno.Text + "', adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'";
任何其他想法我有许多要更新的大表,使用任何其他想法更新都很好。您不需要为每个要更新的字段设置命令。您只需要第一个SET命令 试试这个:
String query = "Update masterusertable set username='" + txtName.Text + "',and password='" + txtpassword.Text + "',and phoneno='" + txtphoneno.Text + "',and adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'";
此外,查看发送到SQL Server的实际查询(如果您使用的是数据库)以及收到的错误也会有所帮助。对于要更新的每个字段,您不需要使用SET命令。您只需要第一个SET命令 试试这个:
String query = "Update masterusertable set username='" + txtName.Text + "',and password='" + txtpassword.Text + "',and phoneno='" + txtphoneno.Text + "',and adress='" + rtxtAdress.Text + "' where userid ='" + txtuserid.Text + "'";
此外,它还可以帮助您查看发送到SQL Server的实际查询(如果您使用的是数据库)以及您收到的错误。您不必对所有SET字段使用和。也无需多次使用SET,您只需通过、
update table set col1=val1,col2=val2.... where coln=valn
您不必对所有设置字段使用和。也无需多次使用SET,您只需通过、
update table set col1=val1,col2=val2.... where coln=valn
这是完全开放的-你应该改为
至于UPDATE
的语法,只有一个SET
子句:
UPDATE masterusertable
SET
username= @username,
password= @password,
phoneno= @phoneno,
adress= @address
WHERE userid = @userid
这是完全开放的-你应该改为
至于UPDATE
的语法,只有一个SET
子句:
UPDATE masterusertable
SET
username= @username,
password= @password,
phoneno= @phoneno,
adress= @address
WHERE userid = @userid
您是使用字符串还是数字作为主键? 语句的
部分正在格式化字符串:
where userid ='" + txtuserid.Text + "'"
这就是你想要的吗
但是既然你说问题是语法错误,而这似乎没有,我想这不是问题所在。你的主键是使用字符串还是数字?
语句的部分正在格式化字符串:
where userid ='" + txtuserid.Text + "'"
这就是你想要的吗
但是,既然您说问题是语法错误,而这似乎没有,我想这不是问题所在。为了可读性,我还建议您在对字符串进行关联时使用string.Format
String query = string.Format(
"Update masterusertable set username='{0}', password='{1}', phoneno='{2}', adress='{3}' where userid ='{4}'",
txtName.Text,
txtpassword.Text,
txtphoneno.Text,
rtxtAdress.Text,
txtuserid.Text
);
为了便于阅读,我还建议您在对字符串进行编码时使用string.Format
String query = string.Format(
"Update masterusertable set username='{0}', password='{1}', phoneno='{2}', adress='{3}' where userid ='{4}'",
txtName.Text,
txtpassword.Text,
txtphoneno.Text,
rtxtAdress.Text,
txtuserid.Text
);
你正在使用什么DBMS?你能发布错误消息吗?帮个忙,然后搜索“避免SQL注入”添加到@Matts comment中-检查参数化查询。您不仅可以避免sql注入攻击,还可以优化查询计划重用,因为您没有使用数据更改查询。第一次更新是不正确的。第二个更好,但如果列名是address(而不是address),则会出现无效列错误。请发布错误消息。您使用的是什么DBMS?您可以发布错误消息吗?请帮个忙,然后搜索添加到@Matts comment的“避免SQL注入”-检查参数化查询。您不仅可以避免sql注入攻击,还可以优化查询计划重用,因为您没有使用数据更改查询。第一次更新是不正确的。第二个更好,但如果列名是address(而不是address),则会出现无效列错误。请发布错误消息。iAM USING ACESS和OLEDB异常由于更新查询中的语法错误而发生。iAM USING ACESS和OLEDB异常由于更新查询中的语法错误而发生