0

每次我运行下面的代码时,它都应该在数据库中找到任何名为“sam”的人,但我不只是想出一个人......

public IEnumerable<Person> GetPersons(string name)
        {
            string PersonID;
            string PersonName;
            var info = new Person[] {};
            try
            {
                string connectionString =
                    @"Password=nottelling;Persist Security Info=True;User ID=nottelling;Initial Catalog=customers;Data Source=db.example.com;";
                using (var connection = new SqlConnection(connectionString))
                {
                    string command = string.Format(@"SELECT Fname, PersonID, Lname FROM Person Where Fname = '{0}'",name);
                    connection.Open();
                    var getperson = new SqlCommand(command, connection);
                    SqlDataReader reader = getperson.ExecuteReader();
                    while (reader.Read())
                    {
                        PersonID = reader["PersonID"].ToString();
                        PersonName = reader["Fname"].ToString();
                        PersonName += " ";
                        PersonName += reader["Lname"].ToString();
                        info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};
                    }
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                PersonID = "Error:";
                PersonName = ex.Message;
                info = new[] {new Person {PersonId = PersonID, Name = PersonName}};

            }
            return info;
        }

和人是:

namespace Calculator.Models
{
    public class Person
    {
        public string PersonId { get; set; }
        public string Name { get; set; }
    }
}

该查询有效,因为我已经在另一个程序上对其进行了多次测试。我认为错误来自:

info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};

因为它替换了 info 的整个值,我怎么会不替换整个值而只是添加它。

4

2 回答 2

2

更新:按照 DanC 的建议,我更改了数组类型

您每次都在分配一个全新的数组...您应该添加到数组中...看看使用类似ArrayList类的东西以便于使用

代替...

public IEnumerable<Person> GetPersons(string name)

改成...

public List<Person> GetPersons(string name)

代替...

var info = new Person[] {};

改成...

List<Person> info = new List<Person>;

代替...

info = new Person[] {new Person {PersonId = PersonID, Name = PersonName}};

改成...

info.Add(new Person {PersonId = PersonID, Name = PersonName});
于 2012-06-22T20:15:57.303 回答
0

做这个:

    public IEnumerable<Person> GetPersons(string name)
    {
        string connectionString = @"Password=nottelling;Persist Security Info=True;User ID=nottelling;Initial Catalog=customers;Data Source=db.example.com;";
        using (var connection = new SqlConnection(connectionString))
        {
            string command = string.Format(@"SELECT Fname, PersonID, Lname FROM Person Where Fname = '{0}'", name);
            connection.Open();

            using (var getperson = new SqlCommand(command, connection))
            using (var reader = getperson.ExecuteReader())
            {
                while (reader.Read())
                {
                    string PersonID = reader["PersonID"].ToString();
                    string PersonName = reader["Fname"].ToString();
                    PersonName += " ";
                    PersonName += reader["Lname"].ToString();
                    yield return new Person {PersonId = PersonID, Name = PersonName};
                }
            }
        }
    }    

我撕掉了那个可怕的异常处理。添加了收益回报,因此您不必构建中间列表。您可以在呼叫站点使用 .ToList() 或 .ToArray() 或其他任何内容。添加了适当的 using 语句来处理您的其他对象。删除了多余的连接关闭。

另外,您真的应该使用参数化查询而不是字符串替换,因为该代码容易受到 sql 注入的影响,但我会为您留下一些工作。:D

于 2012-06-22T20:34:18.503 回答