日期:2014-05-17 浏览次数:20538 次
SqlConnection connection = new SqlConnection();
SqlConnection connectionDetail = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();
connectionDetail.ConnectionString = connectionString;
connectionDetail.Open();
SqlCommand command = connection.CreateCommand();
SqlCommand commandDetail = connectionDetail.CreateCommand();
command.CommandText = "SELECT ID, NAME, NUMBER, DEPARTMENT, AREA FROM W_TELEPHONE WHERE (DEPARTMENT LIKE \'%" + key + "%\')";
commandDetail.CommandText = "SELECT TOP 10 ID, NAME, NUMBER, DEPARTMENT, AREA FROM W_TELEPHONE WHERE (NAME LIKE \'%" + key + "%\')";
SqlDataReader data = command.ExecuteReader();
SqlDataReader dataDetail = commandDetail.ExecuteReader();
List<Phone> phones = new List<Phone>();
if (data.Read())
{
while (data.Read())
{
Phone phonetemp = new Phone();
phonetemp.name = (string)data[1];
phonetemp.number = (string)data[2];
phonetemp.department = (string)data[3];
phonetemp.area = (string)data[4];
phones.Add(phonetemp);
}
}
else if (dataDetail.Read())
{
while (dataDetail.Read())
{
Phone phonetemp = new Phone();
phonetemp.name = (string)data[1];
phonetemp.number = (string)data[2];
phonetemp.department = (string)data[3];
phonetemp.area = (string)data[4];
phones.Add(phonetemp);
}
}
else
{
phones = null;
}
connection.Close();
connectionDetail.Close();
return phones;