我很擅长使用数据库.现在我可以写SELECT,UPDATE,DELETE,和INSERT命令.但我看过很多我们喜欢写的论坛:
SELECT empSalary from employee where salary = @salary
Run Code Online (Sandbox Code Playgroud)
...代替:
SELECT empSalary from employee where salary = txtSalary.Text
Run Code Online (Sandbox Code Playgroud)
为什么我们总是喜欢使用参数以及如何使用它们?
我想知道第一种方法的用途和好处.我甚至听说过SQL注入,但我并不完全理解它.我甚至不知道SQL注入是否与我的问题有关.
在查询1和2中,文本框中的文本都插入到数据库中.这里参数化查询的意义是什么?
1.> -------------
SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Cars " +"VALUES(@TagNbr);" , conn);
cmd.Parameters.Add("@TagNbr", SqlDbType.Int);
cmd.Parameters["@TagNbr"].Value = txtTagNumber.Text;
Run Code Online (Sandbox Code Playgroud)
2.> --------------
int tagnumber = txtTagNumber.Text.ToInt16(); /* EDITED */
INSERT into Cars values(tagnumber.Text); /* then is it the same? */
Run Code Online (Sandbox Code Playgroud)
此外,在这里我将使用正则表达式验证来停止插入非法字符.