120 lines
5.2 KiB
C#
120 lines
5.2 KiB
C#
using System.Data.OleDb;
|
|
using System.Diagnostics;
|
|
|
|
class Download_BD
|
|
{
|
|
static void Main()
|
|
{
|
|
string nome_entrada;
|
|
string strQRY;
|
|
string[] charsToRemove = new string[] { "@", ",", ".", ";", "'", "-", "/" };
|
|
|
|
nome_entrada = "aaa";
|
|
while (nome_entrada.Length != 0)
|
|
{
|
|
Console.WriteLine("Digite 'c' - caso que queira pesquisar por CNPJ ou 'u' - caso que queira pesquisar por UC e pressione Enter");
|
|
Console.WriteLine("Para pesquisar por Razao Social/Nome, escreva o nome da empresa a ser procurada (min: 4 digitos) e pressione Enter - deixe em branco para sair:");
|
|
nome_entrada = Console.ReadLine();
|
|
switch (nome_entrada.ToUpper())
|
|
{
|
|
case "U":
|
|
Console.WriteLine("Digite a UC a ser procurada (completa) e pressione Enter:");
|
|
nome_entrada = Console.ReadLine();
|
|
|
|
if (nome_entrada.Length >= 4)
|
|
{
|
|
strQRY = "SELECT DISTINCT Gestao, Cliente, razao_social, Caminho_NFs FROM Dados_cadastrais WHERE Codigo_Instalacao = " + "\"" + nome_entrada + "\"";
|
|
strQRY += " ORDER BY gestao, cliente, razao_social";
|
|
Acessar_BD(strQRY);
|
|
}
|
|
break;
|
|
|
|
case "C":
|
|
Console.WriteLine("Digite o CNPJ a ser procurado (14 dig) e pressione Enter:");
|
|
nome_entrada = Console.ReadLine();
|
|
|
|
foreach (var c in charsToRemove)
|
|
{
|
|
nome_entrada = nome_entrada.Replace(c, string.Empty);
|
|
}
|
|
|
|
while (nome_entrada.Length < 14)
|
|
{
|
|
nome_entrada = "0" + nome_entrada;
|
|
}
|
|
|
|
strQRY = "SELECT DISTINCT Gestao, Cliente, razao_social, Caminho_NFs FROM Dados_cadastrais WHERE CNPJ_CPF = " + "\"" + nome_entrada + "\"";
|
|
strQRY += " ORDER BY gestao, cliente, razao_social";
|
|
//Console.WriteLine(strQRY);
|
|
Acessar_BD(strQRY);
|
|
break;
|
|
|
|
default:
|
|
if (nome_entrada.Length >= 4)
|
|
{
|
|
strQRY = "SELECT DISTINCT Gestao, Cliente, razao_social, Caminho_NFs FROM Dados_cadastrais WHERE Cliente ALike " + "\"" + "%" + nome_entrada + "%" + "\"";
|
|
strQRY += " UNION " + " SELECT DISTINCT Gestao, Cliente, razao_social, Caminho_NFs FROM Dados_cadastrais WHERE Razao_social ALike " + "\"" + "%" + nome_entrada + "%" + "\"";
|
|
strQRY += " ORDER BY gestao, cliente, razao_social";
|
|
Acessar_BD(strQRY);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
Console.WriteLine("Programa encerrado.");
|
|
return;
|
|
}
|
|
static void Acessar_BD(string strSQL)
|
|
{
|
|
int cont_result;
|
|
string prt_str;
|
|
int n_pasta;
|
|
bool teste_par;
|
|
string teste_cli = "";
|
|
|
|
List<string> caminhos = new List<string>();
|
|
|
|
OleDbConnection conn = new("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb;Jet OLEDB:Database Password=gds21");
|
|
conn.Open();
|
|
OleDbCommand tbEmpresas = new(strSQL, conn);
|
|
OleDbDataReader reader = tbEmpresas.ExecuteReader();
|
|
|
|
prt_str = "\n" + String.Format("{0,-4} | {1,-8} | {2,-25}| {3,-30}", "N", "Gestao", "Nome cliente", "Razao social");
|
|
cont_result = 0;
|
|
while (reader.Read() && cont_result < 11)
|
|
{
|
|
if (reader["Cliente"].ToString() != teste_cli)
|
|
{
|
|
teste_cli = reader["Cliente"].ToString();
|
|
cont_result++;
|
|
prt_str += "\n" + String.Format("{0,-4} | {1,-8} | {2,-25}| {3,-30}", cont_result, reader["gestao"].ToString(), reader["Cliente"].ToString(), reader["Razao_Social"].ToString());
|
|
caminhos.Add(reader["Caminho_NFs"].ToString());
|
|
}
|
|
}
|
|
prt_str += "\n\n";
|
|
conn.Close();
|
|
|
|
if (cont_result >= 10)
|
|
{
|
|
Console.WriteLine("\nForam encontrados mais de {0} resultados para o nome pesquisado, buscou-se por razao social e nome do cliente.\n", cont_result - 1);
|
|
Console.WriteLine("Especificar melhor o nome pesquisado para que os resultados possam sem exibidos.\n");
|
|
}
|
|
else if (cont_result > 0)
|
|
{
|
|
Console.WriteLine("\nForam encontrados {0} resultados para os parametros procurados.\n", cont_result);
|
|
Console.WriteLine(prt_str);
|
|
Console.WriteLine("Digite um numero da lista para acessar a pasta do cliente (só funcionará se possuir acesso):");
|
|
teste_par = Int32.TryParse(Console.ReadLine(), out n_pasta);
|
|
if (teste_par && n_pasta <= cont_result)
|
|
{
|
|
//Console.WriteLine(caminhos[n_pasta - 1]);
|
|
Process.Start("explorer.exe", caminhos[n_pasta - 1]);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Numero invalido");
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|