您当然需要一种更好的方法来构建您的查询。如果没有一定的检查或过滤措施,您不会直接从用户那里获取输入并将其放入您的查询中。这会将您的应用程序暴露给 sql 注入。使用 SQL 参数。试试这个链接作为参考: http: //www.dotnetperls.com/sqlparameter
例子 :
using (SqlCommand command = new SqlCommand("Select * from tableBooks WHERE @Field LIKE @Value", connection))
{
//
// Add new SqlParameter to the command.
//
command.Parameters.Add(new SqlParameter("Field", Textbox1.Text)); // I do not recommend using a textbox and letting the user write anything. You have to limit his choices by the fields in your table. Use a dropdownlist and limit his choices by meaningful fields in your "tableBooks" table.
command.Parameters.Add(new SqlParameter("Value", Textbox2.Text));
//
// Read in the SELECT results.
//
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//GET YOUR BOOK
}
}
请注意我的评论:
// 我不建议使用文本框并让用户写任何东西作为“关键字”。您必须通过表格中的列来限制他的选择。使用下拉列表并通过“tableBooks”表中有意义的选择来限制他的选择。