這裏是我的代碼SqlDataReader對象沒有行發現
public string LeaderIdLookup(string leadername)
{
string step = null;
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select EmpId,Fullname from Employee where FullName like '@LeaderName'";
cmd.Parameters.Add(new SqlParameter("LeaderName", SqlDbType.VarChar));
cmd.Parameters["LeaderName"].Value = leadername.Trim();
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
step = "assigning the value from datareader to the variable lookup as a string (leaderidlookup) ";
if (dr.HasRows)
Lookup = dr[0].ToString();
else
Lookup = "no rows found";
dr.Close();
return Lookup;
}
catch (SqlException ex)
{
Lasterror = string.Format("step {0} threw sql error {1}", step, ex.Message);
Debug.Print(Lasterror);
return string.Empty;
}
catch (Exception ex)
{
Lasterror = string.Format("step {0} threw error {1}", step, ex.Message);
Debug.Print(Lasterror);
return string.Empty;
}
}
的問題是,SqlDataReader的不返回任何行 我有一種預感,它有,因爲當我在那裏硬編碼的名稱與參數替換做而不是使用它的作品完美的參數
我似乎無法弄清楚我會出錯的地方。
圍繞參數的sql刪除引號並匹配參數的名稱:'新的SqlParameter(「@ LeaderName」,SqlDbType.VarChar));' – LarsTech