I have a C# file, that makes some modifications on screen (changes the text on the user interface) but I have some doubts:
- How I can re-use the below methods to be more concise and reusable?
- How I can separate the database methods in another file class?
private void GetDoencaNome(){
using(IDbConnection dbConnection = new SqliteConnection(connectionString)){
dbConnection.Open();
using(IDbCommand dbCommand = dbConnection.CreateCommand()){
string sqlQuery = "SELECT nome FROM doenca WHERE id=@id";
dbCommand.Parameters.Add(new SqliteParameter("@id", doencaId));
dbCommand.CommandText = sqlQuery;
using(IDataReader reader = dbCommand.ExecuteReader()){
while(reader.Read()){
doenca.nome = reader.GetString(0);
}
}
}
}
}
private void GetDoencaDescricao(){
using(IDbConnection dbConnection = new SqliteConnection(connectionString)){
dbConnection.Open();
using(IDbCommand dbCommand = dbConnection.CreateCommand()){
string sqlQuery = "SELECT descricao FROM descricao_doenca WHERE id=@id";
dbCommand.Parameters.Add(new SqliteParameter("@id", doencaId));
dbCommand.CommandText = sqlQuery;
using(IDataReader reader = dbCommand.ExecuteReader()){
while(reader.Read()){
doenca.descricao = reader.GetString(0);
}
}
}
}
}
private void GetDoencaSintomasIds(){
using(IDbConnection dbConnection = new SqliteConnection(connectionString)){
dbConnection.Open();
using(IDbCommand dbCommand = dbConnection.CreateCommand()){
string sqlQuery = "SELECT sintoma_id FROM doenca_sintoma WHERE doenca_id=@id";
dbCommand.Parameters.Add(new SqliteParameter("@id", doencaId));
dbCommand.CommandText = sqlQuery;
using(IDataReader reader = dbCommand.ExecuteReader()){
while(reader.Read()){
listaIds.Add(reader.GetInt32(0));
}
}
}
}
}
private void GetDoencaSintomas(){
using(IDbConnection dbConnection = new SqliteConnection(connectionString)){
dbConnection.Open();
using(IDbCommand dbCommand = dbConnection.CreateCommand()){
string sqlQuery = "SELECT descricao FROM sintoma WHERE";
sqlQuery += " id=" + listaIds[0].ToString();
for(int i = 1; i < listaIds.Count; i++){
sqlQuery += " OR id=" + listaIds[i].ToString();
}
dbCommand.CommandText = sqlQuery;
using(IDataReader reader = dbCommand.ExecuteReader()){
while(reader.Read()){
doenca.sintomas.Add(reader.GetString(0));
}
}
}
}
}