/ / Analisar objeto do sql - c #, sql, sqlite

Analisar objeto de sql - c #, sql, sqlite

Eu criei a tabela e coloquei várias linhas nela. Existe uma maneira de analisar meus próprios objetos da tabela?

class Human
{
string name;
int age;

public Human(string name, int age)
{
this.name = name;
this.age = age;
}
}

-- create table
string sql = "create table human (name varchar(20), age int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

-- insert sample data
string sql = "insert into human (name, age) values ("John", 20)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();

-- get the data back from that table
string sql = "select * from human";
command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();

while (reader.Read())

Agora, como analiso um Human instância do meu leitor?

Respostas:

2 para resposta № 1

sim você pode

É de fato muito simples. Tente o seguinte:

//Create Collection of Human to store the values
List<Human> Humans = new List<Human>();
while (reader.Read())
{
//Create Human Object from Sql Reader
Human h=new Human(reader.getString(0),reader.getInt(1));
//Add the object to collection
Humans.add(h);
}

Humanos Terão todo o Objeto Humano que você obtiver como linhas.


2 para resposta № 2

Sim, há um jeito. Eu sugeriria usar Estrutura de entidade que basicamente permite vincular tipos no seu código a tabelas no banco de dados.

Para o SQLite, aqui estão alguns links para você começar:

http://www.bricelam.net/2012/10/entity-framework-on-sqlite.html

http://erikej.blogspot.co.uk/2014/11/using-sqlite-with-entity-framework-6.html