SqlDataReader的读入名单,其中,串> [英] SqlDataReader to read into List<string>

查看:131
本文介绍了SqlDataReader的读入名单,其中,串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中的方法,从一个WCF服务查询的SQL Server防爆preSS数据库。我不得不使用ADO.NET来做到这一点(再使用LINQ重写一遍以后)。

该方法使用两个字符串( FNAME,LNAME ),然后返回从匹配的记录了健康保险否的属性。我想读这个到一个列表(也有一些其他的attribs检索以及)。

目前的code返回一个空列表。我在哪里去了?

 公开名单<字符串> GetPatientInfo(字符串FNAME,串LNAME)
{
    字符串CONNSTRING =数据源= \\ SQLEX $ P $干燥综合征; AttachDbFilename = C:\\用户\\ XXXX \\ \\文件的Visual Studio 2010 \\ \\项目\\ ADOWebApp \\ ADOWebApp的App_Data \\ ADODatabase。中密度纤维板;集成安全=真;用户实例=真;

    SqlConnection的康恩=新的SqlConnection(CONNSTRING);

    字符串sqlquery的=SELECT *患者来自患者WHERE([名] ='+ FNAME +')和([姓氏] ='+ L-NAME +');
    SqlCommand的命令=新的SqlCommand(sqlquery的,康涅狄格州);
    数据表DT =新的DataTable();

    名单<字符串>结果=新名单,其中,串>();

    使用(康涅狄格州)
    {
        conn.Open();

        使用(SqlDataReader的读卡器= Command.ExecuteReader却())
        {
            而(读卡器= NULL和放大器;!&安培; reader.Read())
            {
               dt.Load(读卡器);
               result.Add(Convert.ToString(阅读器[健康保险否]));
            }
        }
     }

     返回结果;
}
 

解决方案

正在尝试加载数据表通过 DataTable.Load >在一个循环&LT ;.你只需要,一旦。你在循环还使用 reader.Read()。 <一href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx"><$c$c>SqlDataReader.Read()前进读者,但无消耗它的下一个记录。如果你要使用 DataTable.Load 你不需要先读者阅读。所以,你必须要完全删除循环加载该表。

不过既然你想回到你并不需要一个列表里的数据表所有,只是循环读者:

 名单,其中,串&GT;结果=新名单,其中,串&GT;();
使用(康涅狄格州)
{
    conn.Open();
    使用(SqlDataReader的读卡器= Command.ExecuteReader却())
    {
        而(reader.Read())
        {
            result.Add(Convert.ToString(阅读器[健康保险否]));
        }
    }
}
 

除此之外,您是开放的SQL注入没有SQL参数。

I am writing a method in C# to query a SQL Server Express database from a WCF service. I have to use ADO.NET to do this (then rewrite it with LINQ later on).

The method takes two strings (fname, lname) then returns a "Health Insurance NO" attribute from the matching record. 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<. You just need that once. You're also using reader.Read() in the loop. SqlDataReader.Read() advances the reader to the next record without to consume it. If you're going to use DataTable.Load you don't need to read the reader first. 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:

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.

这篇关于SqlDataReader的读入名单,其中,串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆