SqlDataReader找不到行

时间:2016-10-19 21:45:14

标签: c# 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不返回任何行 我有一个预感,它与参数替换有关,因为当我在那里硬编码名称而不是使用参数时它完美地工作

我似乎无法弄清楚我哪里出错了。

3 个答案:

答案 0 :(得分:1)

您需要删除查询中变量名称周围的单引号。否则,您正在进行FullName"@LeaderName"之间的文字比较...这不太可能存在(您没有获得任何行的原因)。您还需要提供带有前导@的参数名称。

cmd.CommandText = "select EmpId,Fullname from Employee where FullName like @LeaderName";
var leaderParameter = cmd.Parameters.Parameters.Add("@LeaderName", System.Data.SqlDbType.NVarChar);
leaderParameter.Value = "%" + leaderName.Trim() + "%";

答案 1 :(得分:1)

您需要删除" LIKE' @LeaderName'"周围的引号并且您必须使用前导@指定参数名称。所以:

... new SqlParameter("@LeaderName", ...

答案 2 :(得分:-1)

您的SqlConnection在哪里?尝试如下所示的内容。 请参阅此处了解连接字符串示例:https://msdn.microsoft.com/en-us/library/ms254500(v=vs.110).aspx#Anchor_2

您的连接字符串将是这样的:“Data Source = localhost; Initial Catalog = MyDataBaseName; Integrated Security = true”(假设您正在连接到localhost,并且您的帐户当然拥有数据库的权限)

try
    {
        using (SqlConnection sqlConnection = new SqlConnection("put your connection string here"))
        {
            sqlConnection.Open();
            using (SqlCommand sqlCommand = new SqlCommand("select EmpId,Fullname from Employee where FullName like @LeaderName", sqlConnection))
            {
                sqlCommand.CommandType = System.Data.CommandType.Text;
                sqlCommand.Parameters.Add("@LeaderName", SqlDbType.VarChar).Value = leadername;
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    //lots of options here, read it how you like...
                    string EmpID = sqlDataReader["EmpID"].ToString();
                    string FullName = sqlDataReader["FullName"].ToString();
                }
            }
        }
    }
 catch (Exception ex) { throw new System.ArgumentException(ex.Message); }