我正在应用程序中添加学生时进行验证。如果我运行以下查询:
SELECT ID FROM Student WHERE Name =' " '+str+' " '
这会生成以下错误:
无效的列名'str'。
我的应用程序将生成DBException。
我该如何解决这个问题?
编辑
String SName=txtBox1.Text;
String sql="select id from student where name = ' "+SName.Trim()+" ' ";
SqlConnection connection = null;
SqlDataReader reader = null;
try
{
connection = GetConnection();
SqlCommand command = new SqlCommand(sql, connection);
if (_sqltransection != null)
{
command.Transaction = _sqltransection;
}
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
throw new DBException(ex);
}
在 txtBox.Text=" '+str+' "
代码中
SELECT ID FROM Student WHERE Name =divyesh
。 - Pரதீப்