C # - How to add results from a SELECT into a List and use the list to Popular a Table

1
All right? I'm new to C # and I'm stagnating on a problem with a project in Visual Studio:

I'm developing a project with DAO (DATA ACCESS OBJETC) standard. I have an interface that is responsible for the methods that will perform the persistence called the InterfaceCourse:

public interface InterfaceDAOCurso{
    void adicionarCurso(Curso c);
    List<Materia> pesquisarCurso(String curso);
}

And the class that implements this DA interface:

public class DAOCurso : InterfaceDAOCurso{

public static String stringConexao = "Data Source=LOCAL-pc ;" +
                             "Initial Catalog=tccDataBase;" +
                             "Trusted_Connection=yes";

public DAOCurso()
{
    //
    // TODO: Add constructor logic here
    //
}

public void adicionarCurso(Curso c)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = stringConexao;
    String sql = "Insert into curso (id,nomeCurso,turno,sigla) values(" + c.getId() + ",'" + c.getNomeCurso() + "','" + c.getTurnoCurso() + "','" + c.getSigla() + "')";
    SqlCommand cmd = new SqlCommand(sql, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Dispose();
}

I would like to implement the method of type List returning a list with the amount of subjects related to this course. However, I'm having problems at the time of popular the list with Query result. (The problem is not the sql command). Is it possible to use result set? How would you implement this function?

Another thing that still is not very clear to me is the Table operation in asp and how could I populate this table with the list that will be returned from my function.

Thanks in advance!

    
asked by anonymous 17.03.2016 / 03:33

1 answer

0

This is an example of a método that returns a list of type string .

In this case I am using a Firebird database.

 public static List<string> Listar(string curso)
        {
            try
            {
                //Variavel local
                FbCommand command;
                FbDataAdapter dataAdapter;

                //Aqui você abre a conexão com o banco

                //Consulta
                command = new FbCommand(@"
                SELECT 
                NOME
                FROM  CURSO", ( sua conexão) ;

                //Intermediario recebe a respota do comandos sql enviado  
                dataAdapter = new FbDataAdapter(command);

                //Estrutura da tabela 
                DataTable objDataTable = new DataTable();

                //Preencher com a estrutura do select enviado com as tuplas
                dataAdapter.Fill(objDataTable);

                //Cria lista
                List<string> ListaDeDados = new List<string>();

                //Percorrer as linhas do datatable para adicionar na lista 
                foreach (DataRow dataRow in objDataTable.Rows)
                {
                    //Adiciona na lista Especificando a clouna 
                    string informação = dataRow["NOME"].ToString();
                    ListaDeDados.Add(informação);
                }

                return ListaDeDados;
            }
            catch (Exception ex)
            {
                //Trata exceção
                throw ex;
            }
            finally
            {
                //Fechar Conexão 
            }
        } 
    
18.03.2016 / 12:43