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

Asked

Viewed 5,121 times

1

Everything all right? I’m new in C# and I’m stuck in a little problem with a project in Visual Studio:

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

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

And the class that implements this Daocurso 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 List type method by returning a list with the amount of subjects related to this course. However, I am having trouble getting the list with Query output. (The problem is not the sql command). Is it possible to use result set? How would the implementation of this function?

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

From now on, thank you very much!

  • I’m not sure I fully understand the question, you want to popular a list with the result of the command ?

  • I may have complicated a little rs. I want to popular a List with the results of a Select. And then use the populated list to fill a table with the data resulting from this select. Did you understand? In java I used the resultset but could not get a similar solution in C#. I am programming for the web and could be Table, gridView or any component of the type to pass the search results.

  • Vitor take a look at the answer I posted I believe this example will help you , I’m sorry for the delay .

1 answer

0


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

In this case I’m using a database Firebird.

 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 
            }
        } 
  • 1

    Your answer gave me a north. I researched a lot of methods and functions of Asp itself to try to accomplish and I decided to use a Gridview as in my answer below:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.