如何在SQL数据库中使用UPDATE语句 [英] How to use UPDATE statement in sql database
问题描述
请使用我想使用C#访问的SQL Sever紧凑型数据库,并希望使用用户在TextBox中输入的值来更新密码表中名为main_password的列.
我尝试过
字符串命令="UPDATE密码SET main_password = TextBox.Text WHERE id = 1"
并使用了ExectueNonQuery,但这没有用.请帮助我使用正确的语法和命令来完成此操作.
Please, I have an SQL Sever compact database that I wish to access using C# and wish to Update a column called main_password in password table with a value the user enters in a TextBox.
I tried
string command ="UPDATE password SET main_password = TextBox.Text WHERE id = 1"
and used ExectueNonQuery but this didn''t work. pls help me with the correct syntax and command to accomplish this.
推荐答案
尝试使用此
try this
//Include Header File
Using System.Data.SqlClient;
//Declare
SqlConnection Con;
SqlCommand Cmd;
Con = new SqlConnection("ConnectionString");
Con.Open();
Cmd = new SqlCommand("Update TableName set Password=@Password where Id= 1",Con);
cmd.parameters.AddwithValue("@Password",TextBox1.text);
Cmd.ExecuteNoQuery();
Con.Close()
尝试:
string command ="UPDATE password SET main_password ='"+ TextBox.Text +"' WHERE id = 1"
顺便说一句,这是对您所做错误的更正.因此,我建议您使用参数化查询来避免问题和SQL注入.
BTW, this was the correction of what you were doing wrong. As such, I would suggest you to use parametrized query to avoid issues and SQL injection.
从
更改sql字符串
Change sql string from
string command ="UPDATE password SET main_password = TextBox.Text WHERE id = 1"
致
To
string command ="UPDATE password SET main_password = ''" + TextBox.Text + "'' WHERE id = 1"
但是使用SqlParameter更好.
But it is way better to use SqlParameter.
这篇关于如何在SQL数据库中使用UPDATE语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!