// Clase para gestion basica de bases de datos de SQLCE
// Aun se esta refinando, puede que algun codigo este de mas.
//————————————————

class SqlCE
    {
        //————————————————————-
       
        // variable de coneccion—————————————
        private SqlCeConnection conn;
       
        // variable de comandos—————————————-
        private SqlCeCommand command;
       
        // variable de construccion de queries————————-
        //private SqlCeCommandBuilder cmdbldr;
       
        // dataAdapter————————————————-
        private SqlCeDataAdapter dataAdapter;
       
        // Lector de datos———————————————
        private  SqlCeDataReader dataReader;
       
        // DataSet—————————————————–
        private CnfgsDataSet ds;
        //————————————————————-

        // Cadena de coneccion
        private string str_coneccion;
        private string query;
       
        public SqlCE()
        {
            //conn = new SqlCeConnection(
            ds = new CnfgsDataSet();
            dataAdapter = new SqlCeDataAdapter();
            query = "select Valor from configuraciones where VarName = ";
        }
        public bool UpdateRegisterVar(string var, string valor)
        {
            int result = 0;
            str_coneccion = @"Data Source=C:\Cnfgs.sdf";
            try
            {
                conn = new SqlCeConnection(str_coneccion);

                string myInsertQuery = "UPDATE configuraciones SET Valor=" + Int32.Parse(valor) + " Where VarName=" + var + ";";
                SqlCeCommand myCommand = new SqlCeCommand(myInsertQuery);

                myCommand.Connection = conn;
                conn.Open();
                result = myCommand.ExecuteNonQuery();
                myCommand.Connection.Close();
                conn.Close();
            }
            catch(SqlCeException sqlceex)
            {
                MessageBox.Show("Ha ocurrido una excepcion en:\n"+
                "SqlCeDataBase"+sqlceex.Message);
            }

            if (result > 0) return true;
            else return false;
        }
        public void ExecuteQuery(string query)
        {
            string connString = @"Data Source=C:\Cnfgs.sdf";

            SqlCeConnection conn = new SqlCeConnection(connString);
            SqlCeCommand cmd = new SqlCeCommand(query, conn);

            conn.Open();
           
            cmd.ExecuteNonQuery();

            conn.Close();

        }
        public string GetRegisterVar(string var)
        {
            string sel = query + @"’"+ var + "’";
            //str_coneccion = @"Data Source=C:\Cnfgs.sdf";
            str_coneccion = @"Data Source=C:\Cnfgs.sdf";
            conn = new SqlCeConnection(str_coneccion);

            dataAdapter = new SqlCeDataAdapter(sel, conn);

            ds = new CnfgsDataSet();

            dataAdapter.Fill(ds,"configuraciones");
            command = new SqlCeCommand(sel, conn);

            conn.Open();

            dataReader = command.ExecuteReader();
            if (dataReader.Read())
            {
                var = dataReader.GetString(0);
            }
            else
            {
                var = "";
            }

            dataReader.Close();
            conn.Close();

            return var;
        }
    }

Anuncios