如何在列表中返回SQL VIEW

时间:2019-05-02 13:59:48

标签: c# .net sql-server sql-view

我编写了一个代码,可以通过一个过程返回到列表,并且可以正常工作,但是现在我需要返回一个视图列表 * SQL Server

我的代码:

public List<Ticket> GetTicket()
{
    List<Ticket> ticketList = new List<Ticket>();

    using (_con = new SqlConnection(_connectionString))
    {
        using (SqlCommand command = new SqlCommand("select * from MyView", _con))
        {
            command.CommandType = CommandType.TableDirect;

            _con.Open();

            SqlDataReader reader = command.ExecuteReader();
            // reader.Load(command.ExecuteReader());

            while (reader.Read())
            {
                Ticket ticket = new Ticket()
                {
                    //TicketNumber = Convert.ToInt32(reader["numeroTicket"]),
                    Priority= Convert.ToString(reader["tpr_txt_prioridade"]),
                    Status = Convert.ToString(reader["status_descri"]),
                    Client = Convert.ToString(reader["cliente"]),
                    Topic = Convert.ToString(reader["topico"]),
                    Responsible= Convert.ToString(reader["usuario_responsavel"]),
                    OpenDate = Convert.ToDateTime(reader["dt_int"]),
                    LastInteraction= Convert.ToDateTime(reader["tkt_dat_prazo"])
                };

                ticketList.Add(ticket);
            }
            return ticketList;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

之前

command.CommandType = CommandType.StoredProcedure;

之后

command.CommandType = CommandType.Text;

我只需要从.StoredProcedure切换到.Text

Ty !!!!!!!!!!!