ASP.NET - Comparar datos de una Tabla en BDD con otra en memoria

 
Vista:

Comparar datos de una Tabla en BDD con otra en memoria

Publicado por Esaú Ruiz (4 intervenciones) el 09/10/2017 19:39:04
Buenas, gente, La duda que tengo es sobre Asp.net en c#.

Primeramente diré que tengo un código donde selecciono un archivo de Excel y este genera automáticamente una tabla en donde mete todos los datos que el archivo contiene (Esto con cualquier archivo de Excel que se le coloque, pero solo voy a trabajar con un mismo archivo y este siempre tiene el mismo formato, las mismas columnas, y los mismos campos). Hasta aquí bien; Ahora, tengo una tabla "Usuarios" en mi base de datos y lo que quiero hacer con ella, es comparar el campo "Curp" de una con la otra, el problema es que no se como hacer la consulta SQL, o mas bien como puedo hacer para comparar la tabla que tengo en la pagina con la tabla de la base de datos.

Aún soy muy novato en esto de Asp.Net y cualquier ayuda explcada me vendría bien, de antemano, gracias.

Este es el código que tengo para leer el archivo Excel y el que genera la tabla automáticamente.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<div class="scrollver pos-grid">
        <div id="mostrar_buscar" style='display:none;'>
            Cadena a buscar <input id="searchTerm" type="text" onkeyup="doSearch()" />
        </div>
        <table id="exceltable" border="1"></table>
    <script>
            function ExportToTable() {
                var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/;
                /*Checks whether the file is a valid excel file*/
                if (regex.test($("#excelfile").val().toLowerCase())) {
                    var xlsxflag = false; /*Flag for checking whether excel is .xls format or .xlsx format*/
                    if ($("#excelfile").val().toLowerCase().indexOf(".xlsx") > 0) {
                        xlsxflag = true;
                    }
                    /*Checks whether the browser supports HTML5*/
                    if (typeof (FileReader) != "undefined") {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            var data = e.target.result;
                            /*Converts the excel data in to object*/
                            if (xlsxflag) {
                                var workbook = XLSX.read(data, { type: 'binary' });
                            }
                            else {
                                var workbook = XLS.read(data, { type: 'binary' });
                            }
                            /*Gets all the sheetnames of excel in to a variable*/
                            var sheet_name_list = workbook.SheetNames;
 
                            var cnt = 0; /*This is used for restricting the script to consider only first sheet of excel*/
                            sheet_name_list.forEach(function (y) { /*Iterate through all sheets*/
                                /*Convert the cell value to Json*/
                                if (xlsxflag) {
                                    var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
                                }
                                else {
                                    var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);
                                }
                                if (exceljson.length > 0 && cnt == 0) {
                                    BindTable(exceljson, '#exceltable');
                                    cnt++;
                                }
                            });
                            $('#exceltable').show();
                        }
                        if (xlsxflag) {/*If excel file is .xlsx extension than creates a Array Buffer from excel*/
                            reader.readAsArrayBuffer($("#excelfile")[0].files[0]);
                        }
                        else {
                            reader.readAsBinaryString($("#excelfile")[0].files[0]);
                        }
                    }
                    else {
                        alert("Lo sentimos, tu navegador no soporta HTML5!");
                    }
                }
                else {
                    alert("Por favor sube un archivo Excel Valido!");
                }
            }
 
        </script>
 
        <script>
            function BindTable(jsondata, tableid) {/*Function used to convert the JSON array to Html Table*/
                var columns = BindTableHeader(jsondata, tableid); /*Gets all the column headings of Excel*/
                for (var i = 0; i < jsondata.length; i++) {
                    var row$ = $('<tr/>');
                    for (var colIndex = 0; colIndex < columns.length; colIndex++) {
                        var cellValue = jsondata[i][columns[colIndex]];
                        if (cellValue == null)
                            cellValue = "";
                        row$.append($('<td/>').html(cellValue));
                    }
                    $(tableid).append(row$);
                }
            }
            function BindTableHeader(jsondata, tableid) {/*Function used to get all column names from JSON and bind the html table header*/
                var columnSet = [];
                var headerTr$ = $('<tr/>');
                for (var i = 0; i < jsondata.length; i++) {
                    var rowHash = jsondata[i];
                    for (var key in rowHash) {
                        if (rowHash.hasOwnProperty(key)) {
                            if ($.inArray(key, columnSet) == -1) {/*Adding each unique column names to a variable array*/
                                columnSet.push(key);
                                headerTr$.append($('<th/>').html(key));
                            }
                        }
                    }
                }
                $(tableid).append(headerTr$);
                return columnSet;
            }
    function doSearch() {
                var tableReg = document.getElementById('exceltable');
                var searchText = document.getElementById('searchTerm').value.toLowerCase();
                var cellsOfRow = "";
                var found = false;
                var compareWith = "";
 
                // Recorremos todas las filas con contenido de la tabla
 
                for (var i = 1; i < tableReg.rows.length; i++) {
                    cellsOfRow = tableReg.rows[i].getElementsByTagName('td');
                    found = false;
                    // Recorremos todas las celdas
 
                    for (var j = 0; j < cellsOfRow.length && !found; j++) {
                        compareWith = cellsOfRow[j].innerHTML.toLowerCase();
                        // Buscamos el texto en el contenido de la celda
 
                        if (searchText.length == 0 || (compareWith.indexOf(searchText) > -1)) {
                            found = true;
                        }
                    }
                    if (found) {
                        tableReg.rows[i].style.display = '';
                    } else {
                        // si no ha encontrado ninguna coincidencia, esconde la
 
                        // fila de la tabla
 
                        tableReg.rows[i].style.display = 'none';
                    }
                }
            }
</script>
/* Esto es para mostrar unas partes del HTML, no es importante en este caso, pero lo coloco por que va dentro del código que coloque. */
<script type="text/javascript">
            function ocultar() {
                document.getElementById('Ocultar').style.display = 'none';
            }
            function mostrar() {
                document.getElementById('Pos-Boton2').style.display = 'block';
            }
            function mostrar_buscar() {
                document.getElementById('mostrar_buscar').style.display = 'block';
            }
        </script>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder