简体   繁体   English

SqlDataReader读入List <string>

[英]SqlDataReader to read into List<string>

I am writing a method in C# to query a SQL Server Express database from a WCF service. 我正在用C#编写一个方法来从WCF服务查询SQL Server Express数据库。 I have to use ADO.NET to do this (then rewrite it with LINQ later on). 我必须使用ADO.NET来执行此操作(然后使用LINQ重写它)。

The method takes two strings ( fname, lname ) then returns a "Health Insurance NO" attribute from the matching record. 该方法接受两个字符串( fname, lname ),然后从匹配记录中返回“Health Insurance NO”属性。 I want to read this into a list (there are some other attribs to retrieve as well). 我想把它读成一个列表(还有其他一些要检索的属性)。

The current code returns an empty list. 当前代码返回一个空列表。 Where am I going wrong? 我哪里错了?

public List<string> GetPatientInfo(string fname, string lname)
{
    string connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\xxxx\\Documents\\Visual Studio 2010\\Projects\\ADOWebApp\\ADOWebApp\\App_Data\\ADODatabase.mdf;Integrated Security=True;User Instance=True";

    SqlConnection conn = new SqlConnection(connString);

    string sqlquery = "SELECT Patient.* FROM Patient WHERE ([First Name] = '"+fname+"') AND ([Last Name] = '"+lname+"')";
    SqlCommand command = new SqlCommand(sqlquery, conn);
    DataTable dt = new DataTable();

    List<string> result = new List<string>();

    using (conn)
    {
        conn.Open();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader != null && reader.Read())
            {
               dt.Load(reader);
               result.Add(Convert.ToString(reader["Health Insurance NO"]));
            }
        }
     }

     return result;
}

You are trying to load a DataTable via DataTable.Load >in a loop<. 您正在尝试通过DataTable.Load >在循环<中加载DataTable You just need that once. 你只需要一次。 You're also using reader.Read() in the loop. 你也在循环中使用reader.Read() SqlDataReader.Read() advances the reader to the next record without to consume it. SqlDataReader.Read()使读者前进到下一条记录而不使用它。 If you're going to use DataTable.Load you don't need to read the reader first. 如果您打算使用DataTable.Load ,则无需先读取阅读器。 So you just have to remove the loop completely to load the table. 所以你只需要完全删除循环来加载表。

But since you want to return a list you don't need the DataTable at all, just loop the reader: 但是既然你想要返回一个列表,你根本不需要DataTable ,只需循环读者:

List<string> result = new List<string>();
using (conn)
{
    conn.Open();
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while(reader.Read())
        {
            result.Add(Convert.ToString(reader["Health Insurance NO"]));
        }
    }
}

Apart from that, you are open for sql-injection without sql-parameters. 除此之外,你可以在没有sql参数的情况下进行sql-injection。

I would do it this way: 我会这样做:

 conn.Open();
 using (SqlDataReader reader = command.ExecuteReader())
 {
     dt.Load(reader);                  
 }

 foreach (var row in dt.AsEnumerable())
 {
     result.Add(row["Health Insurance NO"].ToString());
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM