任何人都可以帮助我选择查询的语法吗?

1 c# sql

我在做

string sql = "select * from publisher where title like "'"+tbproperty.text+";
Run Code Online (Sandbox Code Playgroud)

但它不起作用!

问候..

Kyl*_*ndo 9

用途SqlParameter:

SqlCommand cmd = new SqlCommand("select * from publisher where title like @title");
cmd.Parameters.AddWithValue("@title", tbProperty.Text);
Run Code Online (Sandbox Code Playgroud)

如果需要向参数添加更多内容,请执行以下操作(例如:输出参数):

SqlParameter param = new SqlParameter("@param ", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(param);
Run Code Online (Sandbox Code Playgroud)

这意味着您不需要构建字符串本身并停止SQL注入.

  • +1希望我能+10000你的答案!! 始终始终(并且没有例外)使用参数化查询. (3认同)