-
-
Save denisferrari/1b2287c8c0f9ccc37855 to your computer and use it in GitHub Desktop.
DevInCachu 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "Server=127.0.0.1;Database=devincachu;Uid=root;Pwd=;"; | |
var filePath = @"C:\Users\Denis\Desktop\DevInCachu\DevInCachu.Playground\prospects.txt"; | |
var connection = new MySqlConnection(connectionString); | |
connection.Open(); | |
var linhas = File.ReadAllLines(filePath); | |
var linhasInvalidas = 0; | |
foreach (var linha in linhas) | |
{ | |
if (!string.IsNullOrEmpty(linha.Trim())) | |
{ | |
var valores = linha.Split(';'); | |
var nome = valores[0].Trim(); | |
var email = valores[1].Trim(); | |
var cpf = valores[2].Trim(); | |
var cidade = valores[3].Trim(); | |
if (ValidaCPF.IsCpf(cpf)) | |
{ | |
var command = new MySqlCommand(@"INSERT INTO Prospects( Nome, Email, Cidade, CPf) | |
VALUES(@Nome,@Email,@Cidade,@Cpf)", connection); | |
command.Parameters.AddWithValue("Nome", nome); | |
command.Parameters.AddWithValue("Email", email); | |
command.Parameters.AddWithValue("Cidade", cidade); | |
command.Parameters.AddWithValue("Cpf", cpf); | |
command.ExecuteNonQuery(); | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
connection.Close(); | |
Console.WriteLine("Número de linhas: " + linhas.Count()); | |
Console.WriteLine("Linhas importadas: " + (linhas.Count() - linhasInvalidas)); | |
Console.WriteLine("Linhas vazias ou inválidas: " + linhasInvalidas); | |
Console.ReadLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Prospect | |
{ | |
public string Nome { get; set; } | |
public string Email { get; set; } | |
public string Cpf { get; set; } | |
public string Cidade { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "Server=127.0.0.1;Database=devincachu;Uid=root;Pwd=;"; | |
var filePath = @"C:\Users\Denis\Desktop\DevInCachu\DevInCachu.Playground\prospects.txt"; | |
var connection = new MySqlConnection(connectionString); | |
connection.Open(); | |
var linhas = File.ReadAllLines(filePath); | |
var linhasInvalidas = 0; | |
foreach (var linha in linhas) | |
{ | |
if (!string.IsNullOrEmpty(linha.Trim())) | |
{ | |
var valores = linha.Split(';'); | |
var prospect = new Prospect() | |
{ | |
Nome = valores[0].Trim(), | |
Email = valores[1].Trim(), | |
Cpf = valores[2].Trim(), | |
Cidade = valores[3].Trim() | |
}; | |
if (ValidaCPF.IsCpf(prospect.Cpf)) | |
{ | |
var command = new MySqlCommand(@"INSERT INTO Prospects( Nome, Email, Cidade, CPf) | |
VALUES(@Nome,@Email,@Cidade,@Cpf)", connection); | |
command.Parameters.AddWithValue("Nome", prospect.Nome); | |
command.Parameters.AddWithValue("Email", prospect.Email); | |
command.Parameters.AddWithValue("Cidade", prospect.Cidade); | |
command.Parameters.AddWithValue("Cpf", prospect.Cpf); | |
command.ExecuteNonQuery(); | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
connection.Close(); | |
Console.WriteLine("Número de linhas: " + linhas.Count()); | |
Console.WriteLine("Linhas importadas: " + (linhas.Count() - linhasInvalidas)); | |
Console.WriteLine("Linhas vazias ou inválidas: " + linhasInvalidas); | |
Console.ReadLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Prospect | |
{ | |
public string Nome { get; set; } | |
public string Email { get; set; } | |
public string Cpf { get; set; } | |
public string Cidade { get; set; } | |
} | |
public class Prospects | |
{ | |
private MySqlConnection Connection { get; set; } | |
public Prospects(MySqlConnection connection) | |
{ | |
this.Connection = connection; | |
} | |
public void Salvar(Prospect prospect) | |
{ | |
var command = new MySqlCommand(@"INSERT INTO Prospects( Nome, Email, Cidade, CPf) | |
VALUES(@Nome,@Email,@Cidade,@Cpf)", this.Connection); | |
command.Parameters.AddWithValue("Nome", prospect.Nome); | |
command.Parameters.AddWithValue("Email", prospect.Email); | |
command.Parameters.AddWithValue("Cidade", prospect.Cidade); | |
command.Parameters.AddWithValue("Cpf", prospect.Cpf); | |
command.ExecuteNonQuery(); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "Server=127.0.0.1;Database=devincachu;Uid=root;Pwd=;"; | |
var filePath = @"C:\Users\Denis\Desktop\DevInCachu\DevInCachu.Playground\prospects.txt"; | |
var connection = new MySqlConnection(connectionString); | |
var prospects = new Prospects(connection); | |
connection.Open(); | |
var linhas = File.ReadAllLines(filePath); | |
var linhasInvalidas = 0; | |
foreach (var linha in linhas) | |
{ | |
if (!string.IsNullOrEmpty(linha.Trim())) | |
{ | |
var valores = linha.Split(';'); | |
var prospect = new Prospect() | |
{ | |
Nome = valores[0].Trim(), | |
Email = valores[1].Trim(), | |
Cpf = valores[2].Trim(), | |
Cidade = valores[3].Trim() | |
}; | |
if (ValidaCPF.IsCpf(prospect.Cpf)) | |
{ | |
prospects.Salvar(prospect); | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
connection.Close(); | |
Console.WriteLine("Número de linhas: " + linhas.Count()); | |
Console.WriteLine("Linhas importadas: " + (linhas.Count() - linhasInvalidas)); | |
Console.WriteLine("Linhas vazias ou inválidas: " + linhasInvalidas); | |
Console.ReadLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Prospect | |
{ | |
public string Nome { get; set; } | |
public string Email { get; set; } | |
public string Cpf { get; set; } | |
public string Cidade { get; set; } | |
} | |
public class Prospects | |
{ | |
private MySqlConnection Connection { get; set; } | |
public Prospects(MySqlConnection connection) | |
{ | |
this.Connection = connection; | |
} | |
public void Salvar(Prospect prospect) | |
{ | |
var command = new MySqlCommand(@"INSERT INTO Prospects( Nome, Email, Cidade, CPf) | |
VALUES(@Nome,@Email,@Cidade,@Cpf)", this.Connection); | |
command.Parameters.AddWithValue("Nome", prospect.Nome); | |
command.Parameters.AddWithValue("Email", prospect.Email); | |
command.Parameters.AddWithValue("Cidade", prospect.Cidade); | |
command.Parameters.AddWithValue("Cpf", prospect.Cpf); | |
command.ExecuteNonQuery(); | |
} | |
} | |
public class ProspectsEmTxt | |
{ | |
public int LinhasNoArquivo { get; private set; } | |
public int LinhasInvalidas { get; private set; } | |
public int LinhasValidas { get; private set; } | |
public string Caminho { get; private set; } | |
public ProspectsEmTxt(string caminho) | |
{ | |
this.Caminho = caminho; | |
this.LinhasInvalidas = 0; | |
} | |
public List<Prospect> ObterProspects() | |
{ | |
var prospects = new List<Prospect>(); | |
var linhas = File.ReadAllLines(this.Caminho); | |
var linhasInvalidas = 0; | |
foreach (var linha in linhas) | |
{ | |
if (!string.IsNullOrEmpty(linha.Trim())) | |
{ | |
var valores = linha.Split(';'); | |
var prospect = new Prospect() | |
{ | |
Nome = valores[0].Trim(), | |
Email = valores[1].Trim(), | |
Cpf = valores[2].Trim(), | |
Cidade = valores[3].Trim() | |
}; | |
if (ValidaCPF.IsCpf(prospect.Cpf)) | |
{ | |
prospects.Add(prospect); | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
this.LinhasNoArquivo = linhas.Count(); | |
this.LinhasInvalidas = linhasInvalidas; | |
this.LinhasValidas = this.LinhasNoArquivo - this.LinhasInvalidas; | |
return prospects; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "Server=127.0.0.1;Database=devincachu;Uid=root;Pwd=;"; | |
var filePath = @"C:\Users\Denis\Desktop\DevInCachu\DevInCachu.Playground\prospects.txt"; | |
var connection = new MySqlConnection(connectionString); | |
var prospectsEmTxt = new ProspectsEmTxt(filePath); | |
var prospects = new Prospects(connection); | |
connection.Open(); | |
foreach (var prospect in prospectsEmTxt.ObterProspects()) | |
{ | |
prospects.Salvar(prospect); | |
} | |
connection.Close(); | |
Console.WriteLine("Número de linhas: " + prospectsEmTxt.LinhasNoArquivo); | |
Console.WriteLine("Linhas importadas: " + prospectsEmTxt.LinhasValidas); | |
Console.WriteLine("Linhas vazias ou inválidas: " + prospectsEmTxt.LinhasInvalidas); | |
Console.ReadLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Prospect | |
{ | |
public string Nome { get; set; } | |
public string Email { get; set; } | |
public Cpf Cpf { get; set; } | |
public string Cidade { get; set; } | |
} | |
public class Prospects | |
{ | |
private MySqlConnection Connection { get; set; } | |
public Prospects(MySqlConnection connection) | |
{ | |
this.Connection = connection; | |
} | |
public void Salvar(Prospect prospect) | |
{ | |
var command = new MySqlCommand(@"INSERT INTO Prospects( Nome, Email, Cidade, CPf) | |
VALUES(@Nome,@Email,@Cidade,@Cpf)", this.Connection); | |
command.Parameters.AddWithValue("Nome", prospect.Nome); | |
command.Parameters.AddWithValue("Email", prospect.Email); | |
command.Parameters.AddWithValue("Cidade", prospect.Cidade); | |
command.Parameters.AddWithValue("Cpf", prospect.Cpf.ToString()); | |
command.ExecuteNonQuery(); | |
} | |
} | |
public class ProspectsEmTxt | |
{ | |
public int LinhasNoArquivo { get; private set; } | |
public int LinhasInvalidas { get; private set; } | |
public int LinhasValidas { get; private set; } | |
public string Caminho { get; private set; } | |
public ProspectsEmTxt(string caminho) | |
{ | |
this.Caminho = caminho; | |
this.LinhasInvalidas = 0; | |
} | |
public List<Prospect> ObterProspects() | |
{ | |
var prospects = new List<Prospect>(); | |
var linhas = File.ReadAllLines(this.Caminho); | |
var linhasInvalidas = 0; | |
foreach (var linha in linhas) | |
{ | |
if (!string.IsNullOrEmpty(linha.Trim())) | |
{ | |
var valores = linha.Split(';'); | |
var cpf = new Cpf(valores[2].Trim()); | |
if (cpf.EhValido()) | |
{ | |
var prospect = new Prospect() | |
{ | |
Nome = valores[0].Trim(), | |
Email = valores[1].Trim(), | |
Cpf = cpf, | |
Cidade = valores[3].Trim() | |
}; | |
prospects.Add(prospect); | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
else | |
{ | |
linhasInvalidas++; | |
} | |
} | |
this.LinhasNoArquivo = linhas.Count(); | |
this.LinhasInvalidas = linhasInvalidas; | |
this.LinhasValidas = this.LinhasNoArquivo - this.LinhasInvalidas; | |
return prospects; | |
} | |
} | |
public struct Cpf | |
{ | |
private string cpf { get; set; } | |
public Cpf(string cpf) | |
: this() | |
{ | |
this.cpf = cpf; | |
} | |
public bool EhValido() | |
{ | |
return ValidaCPF.IsCpf(this.cpf); | |
} | |
public override string ToString() | |
{ | |
return this.cpf; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var connectionString = "Server=127.0.0.1;Database=devincachu;Uid=root;Pwd=;"; | |
var filePath = @"C:\Users\Denis\Desktop\DevInCachu\DevInCachu.Playground\prospects.txt"; | |
var connection = new MySqlConnection(connectionString); | |
var prospectsEmTxt = new ProspectsEmTxt(filePath); | |
var prospects = new Prospects(connection); | |
connection.Open(); | |
foreach (var prospect in prospectsEmTxt.ObterProspects()) | |
{ | |
prospects.Salvar(prospect); | |
} | |
connection.Close(); | |
Console.WriteLine("Número de linhas: " + prospectsEmTxt.LinhasNoArquivo); | |
Console.WriteLine("Linhas importadas: " + prospectsEmTxt.LinhasValidas); | |
Console.WriteLine("Linhas vazias ou inválidas: " + prospectsEmTxt.LinhasInvalidas); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment