C#SqlDataReader只检索一行

时间:2018-02-17 10:00:22

标签: c# sql-server

我有一个问题,我列出了100多名学生,但读者只检索了表格中的最后一名学生。

string selectQuery = "SELECT * FROM Students WHERE firstName = @first AND lastName = @last";

using (SqlCommand sqlCommand = new SqlCommand(selectQuery, sqlConnection))
{
    sqlCommand.Parameters.Add("@first", SqlDbType.VarChar);
    sqlCommand.Parameters.Add("@last", SqlDbType.VarChar);

    foreach (Student student in studentList)
    {
        sqlCommand.Parameters["@first"].Value = student.first;
        sqlCommand.Parameters["@last"].Value = student.last;
    }

    using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
    {
        while (sqlReader.Read())
        {
            Console.WriteLine(sqlReader["firstName"].ToString());
            Console.WriteLine(sqlReader["lastName"].ToString());
        }
    }
}

2 个答案:

答案 0 :(得分:2)

如果您要检索所有学生的值,则需要在foreach Student循环中执行SQL

string selectQuery = "SELECT * FROM Students WHERE firstName = @first AND lastName = @last";

using (SqlCommand sqlCommand = new SqlCommand(selectQuery, sqlConnection))
{
    sqlCommand.Parameters.Add("@first", SqlDbType.VarChar);
    sqlCommand.Parameters.Add("@last", SqlDbType.VarChar);

    foreach (Student student in studentList)
    {
        sqlCommand.Parameters["@first"].Value = student.first;
        sqlCommand.Parameters["@last"].Value = student.last;

        using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
        {
             while (sqlReader.Read())
             {
                 Console.WriteLine(sqlReader["firstName"].ToString());
                 Console.WriteLine(sqlReader["lastName"].ToString());
             }
        }
    }
}

但这真的没有多大意义......你真的试图实现什么?您按名字和姓氏进行搜索 - 您从studentList提供 - 然后您还只输出您检索到的姓名和姓氏 - 这与您的相同传入 - 所以为什么甚至去数据库?

更新:,因为@PatrickArtner正确评论 - 创建和处理100个SqlDataReader实例并不理想。最好将这个选择过程放在一个存储过程中,该存储过程只有一个SELECT和一个table-valued parameter,它将保存100(或更多)学生ID或信息,根据您的需要选择您的学生,然后使用单个 SqlDataReader对象在C#中从该存储过程迭代结果集。

答案 1 :(得分:0)

问题的原因是您反复覆盖单个2个参数。请参阅代码中的注释:

string selectQuery = "SELECT * FROM Students WHERE firstName=@first and lastName=@last";
using (SqlCommand sqlCommand = new SqlCommand(selectQuery, sqlConnection))
{
    sqlCommand.Parameters.Add("@first", SqlDbType.VarChar);
    sqlCommand.Parameters.Add("@last", SqlDbType.VarChar);
    foreach (Student student in studentList) // addds 100 students
    {
        // overwrites the same parameter again and again.... so its only the last one
        sqlCommand.Parameters["@first"].Value = student.first;
        sqlCommand.Parameters["@last"].Value = student.last;
    }

    using (SqlDataReader sqlReader = sqlCommand.ExecuteReader()) 
    {// the query uses only 2 params so you need to change the query and the adding
        while (sqlReader.Read())
        {
            Console.WriteLine(sqlReader["firstName"].ToString());
            Console.WriteLine(sqlReader["lastName"].ToString());
        }
    }
 } 

解决方案使用来自您的数据的更复杂的WHERE条件构建以及数据所需的SqlParameters,通过一个查询搜索所有这些查询:

// create all the variable names
var firstVarName = Enumerable.Range(1, studentList.Count)
    .Select(i => $"@first_{i}")
    .ToList();
var lastVarName = Enumerable.Range(1, studentList.Count)
    .Select(i => $"@last_{i}")
    .ToList();

var cond = "firstName = {0} and lastName = {1}";

var whereOred = new StringBuilder("WHERE 1 = 0  -- false, just ease of formatting\n");

for (int i = 0; i < firstVarName.Count; i++)
{
    whereOred.AppendLine("   OR " + string.Format(cond, firstVarName[i], lastVarName[i]));
}

// adapt query to search all variable names
string selectQuery = $@"
SELECT *
FROM Students
{whereOred.ToString()}";

Console.WriteLine(selectQuery);
using (SqlCommand sqlCommand = new SqlCommand(selectQuery, sqlConnection))
{
    // add all the variable names with values from studentList
    for(int i=0;i<studentList.Count;i++) 
    {
        sqlCommand.Parameters.Add(firstVarName[i], SqlDbType.VarChar);
        sqlCommand.Parameters.Add(lastVarName[i], SqlDbType.VarChar);
        sqlCommand.Parameters[firstVarName[i]].Value = studentList[i].first;
        sqlCommand.Parameters[lastVarName[i]].Value = studentList[i].last;
    }

    using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
    { 
        if (sqlReader.HasRows)
            while (sqlReader.Read())
            {
                Console.WriteLine(sqlReader["firstName"].ToString());
                Console.WriteLine(sqlReader["lastName"].ToString());
            }
        else Console.WriteLine("No match.");
    }
}
为我的demodata创建

SqlStatement

SELECT *
FROM Students
WHERE 1 = 0  -- false, just ease of formatting
   OR firstName = @first_1 and lastName = @last_1
   OR firstName = @first_2 and lastName = @last_2
   OR firstName = @first_3 and lastName = @last_3
   OR firstName = @first_4 and lastName = @last_4
   OR firstName = @first_5 and lastName = @last_5
   OR firstName = @first_6 and lastName = @last_6
   OR firstName = @first_7 and lastName = @last_7
   OR firstName = @first_8 and lastName = @last_8
   OR firstName = @first_9 and lastName = @last_9
   OR firstName = @first_10 and lastName = @last_10
   OR firstName = @first_11 and lastName = @last_11
   OR firstName = @first_12 and lastName = @last_12
   OR firstName = @first_13 and lastName = @last_13
   OR firstName = @first_14 and lastName = @last_14
   OR firstName = @first_15 and lastName = @last_15
   OR firstName = @first_16 and lastName = @last_16
   OR firstName = @first_17 and lastName = @last_17
   OR firstName = @first_18 and lastName = @last_18
   OR firstName = @first_19 and lastName = @last_19
   OR firstName = @first_20 and lastName = @last_20
添加

SqlParameters 以执行我的demodata: paramlist in debug mode

我用过

public class Student { public string first; public string last; }

var studentList = Enumerable.Range(1, 20)
    .Select(i => new Student { first = $"firstname {i}", last = $"lastname {i}" })
    .ToList();

创建一些演示学习列表。