WPF and SqLite Desktop Application

0

I am very new programming and very very inexperienced.

I started to program a desktop application in WPF with C # and as a database I use SQLite since the data I use is very simple and I do not require a more powerful database manager.

Within my solution I decide to include a class to manipulate the database which is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;

namespace DashEnt
{
    class DbMani
   {
    private String cadena = "data source=DbDash.db";
    public SQLiteConnection cn;
    private SQLiteCommandBuilder cmb;
    public DataSet ds = new DataSet();
    public SQLiteDataAdapter da;
    public SQLiteCommand comando;

    private void conectar()
    {
        cn = new SQLiteConnection(cadena);
    }

    public DbMani()
    {
        conectar();
    }

    //Metodo para Consultas

    public void consultar(string sql, string tabla)
    {
        ds.Tables.Clear();
        da = new SQLiteDataAdapter(sql, cn);
        cmb = new SQLiteCommandBuilder(da);
        da.Fill(ds, tabla);
    }

    //Metodo para Eliminar

    public bool eliminar(string tabla, string condicion)
    {
        cn.Open();
        string sql = "delete from " + tabla + " where " + condicion;
        comando = new SQLiteCommand(sql, cn);
        int i = comando.ExecuteNonQuery();
        cn.Close();
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //Metodo para Actualizar

    public bool actualizar(string tabla, string campos, string condicion)
    {
        cn.Open();
        string sql = "update " + tabla + " set " + campos + " where " + condicion;
        comando = new SQLiteCommand(sql, cn);
        int i = comando.ExecuteNonQuery();
        cn.Close();
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //Metodo consultas para combos

    public DataTable consucom(string tabla)
    {
        string sql = "select * from " + tabla;
        da = new SQLiteDataAdapter(sql, cn);
        DataSet dts = new DataSet();
        da.Fill(dts, tabla);
        DataTable dt = new DataTable();
        dt = dts.Tables[tabla];
        return dt;
    }

    //Metodo para insertar datos

    public bool insertar(string sql)
    {
        cn.Open();
        comando = new SQLiteCommand(sql, cn);
        int i = comando.ExecuteNonQuery();
        cn.Close();
        if (i > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
  }
}

So far everything seems normal, in the main form I have a datagrid, I urge the class mentioned above as follows:

DbMani obDatos = new DbMani();

When loading the form I want the datagrid to be filled in, for this I do the following:

private void MainWindow_Load(object sender, RoutedEventArgs e)
    {
        obDatos.consultar("select * from TabBitacora", "TabBitacora");
        this.dataApp.ItemsSource = obDatos.ds.Tables["TabBitacora"];

    }

and here is the error that tells me:

Sorry for the bad catch, basically what the error tells me is:

  

(Field) System.Data.DataSet.DbMani.ds Unable to convert   implicitly type System.Data.DataTable in   System.Collections. | Enumerable. There is already an explicit conversion   (check if you are missing some conversion)

I'm probably making a fool of myself, could someone tell me which?

Thank you very much.

Greetings.

    
asked by Ivangilfer1975 25.06.2018 в 18:26
source

1 answer

0

I do it all by means of a DataReader and it works great for me, however I have the fixed columns in the grid and that's why I do it this way

SQL = "Select act,cnf,nam,pul,pud,dol,dod,car,rat,ins,cid,sor,fecha_procesado from tb_wizard_layout where impreso IS NULL";
    try
    {
        oComando.Connection = CC;
        oComando.CommandText = SQL;
        oDataReader = oComando.ExecuteReader();
        oComando.Dispose();
    }
    catch (NpgsqlException ErrorCons)
    {
        MessageBox.Show("ERROR: " + ErrorCons.Message);
    }
    while(oDataReader.Read())
    {
        DGVReport.Rows.Add(oDataReader["act"], oDataReader["cnf"], oDataReader["nam"], oDataReader["pul"], oDataReader["pud"], oDataReader["dol"], oDataReader["dod"], oDataReader["car"], oDataReader["rat"], oDataReader["cid"], oDataReader["sor"], oDataReader["fecha_procesado"]);              
    }
    CC.Close();

But earlier to do it as you want, you can also do it in the following way

    SQL = "Select act,cnf,nam,pul,pud,dol,dod,car,flt,rat,ins,cid,mop,cmp,ad1,ad2,ad3,dlc,sor,fecha_procesado from tb_wizard_layout where impreso IS NULL";
    NpgsqlDataAdapter DataAdapter = new NpgsqlDataAdapter(SQL, CC);
    DataAdapter.Fill(ReportDataSet);
    DGVReport.DataSource = ReportDataSet.Tables[0];
    CC.Close();

This way you load in automatic the data on a clean grid

    
answered by 11.07.2018 в 19:37