如果没有看到更多代码,可能是您只填充了fname
or变量中的一个。lname
假设是这种情况,您可以使用此代码。
// this assumes that you have only have 1 input for both the first name and lastname
// and are storing it in a variable named searchForName
using (var connection = new SqlConnection("YOUR_CONFIGURATION_CONNECTION_STRING"))
{
using (var command = new SqlCommand("SELECT opd_id AS [OPD No], opd_date AS DATE, opd_dpt AS DEPARTMENT, opd_pfname + ' ' + opd_plname AS [Patient NAME], opd_age AS AGE, opd_gender AS GENDER, opd_mob AS [MOBILE NO], opd_fthrname AS [FATHER NAME], opd_hsbndname AS [HUSBAND NAME] FROM tbl_OPD WHERE (opd_pfname like @p0 OR opd_plname like @p0) ORDER BY DATE DESC", connection))
{
command.Parameters.Add(new SqlParameter("@p0", string.Format("%{0}%", searchForName)));
// rest of your code here E.G., sComm.ExecuteNonQuery();
}
}
如果您知道两个变量都已填充,请尝试此代码块
using (var connection = new SqlConnection("YOUR_CONFIGURATION_CONNECTION_STRING"))
{
using (var command = new SqlCommand("SELECT opd_id AS [OPD No], opd_date AS DATE, opd_dpt AS DEPARTMENT, opd_pfname + ' ' + opd_plname AS [Patient NAME], opd_age AS AGE, opd_gender AS GENDER, opd_mob AS [MOBILE NO], opd_fthrname AS [FATHER NAME], opd_hsbndname AS [HUSBAND NAME] FROM tbl_OPD WHERE (opd_pfname like @p0 OR opd_plname like @p1) ORDER BY DATE DESC", connection))
{
command.Parameters.Add(new SqlParameter("@p0", string.Format("%{0}%", searchForFirstName)));
command.Parameters.Add(new SqlParameter("@p1", string.Format("%{0}%", searchForLastName)));
// rest of your code here E.G., sComm.ExecuteNonQuery();
}
}
PS。正如其他人所说,此处显示的代码有些不安全,您应该尽一切努力避免 Sql Injection 攻击。