/ / Analizuj obiekt z sql - c #, sql, sqlite

Przetwarzanie obiektu z sql - c #, sql, sqlite

Utworzyłem tabelę i umieściłem w niej kilka wierszy. Czy istnieje sposób na parsowanie własnych obiektów ze stołu?

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())

Jak teraz przeanalizować a Human wystąpienie od mojego czytelnika?

Odpowiedzi:

2 dla odpowiedzi № 1

tak, możesz

To bardzo prosty fakt. Spróbuj tego:

//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);
}

Ludzie będą mieli cały Ludzki Obiekt, który dostaniesz jako rzędy.


2 dla odpowiedzi nr 2

Tak, jest sposób. Sugerowałbym użycie Struktura Entity co zasadniczo umożliwia powiązanie typów w kodzie z tabelami w bazie danych.

W przypadku SQLite, oto kilka linków, które pomogą Ci zacząć:

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