Boa noite! To acompanhando um tuto que ensina fazer um crud, porém, na hora do cadastrar, ele não registra. (Primeiro tópico aqui no forum)
O video é esse:
E assim está o meu Crud:
private void btncadastrar_Click(object sender, EventArgs e)
{
string conexao = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source="+ Application.StartupPath +@"\Cadastro de Clientes.accdb";
string consulta = "INSERT INTO Clientes (nome, nome pet, cpf, telefone, celular, endereço) values (@nome, @nome pet, @cpf, @telefone, @celular, @endereco)";
OleDbConnection con = new OleDbConnection(conexao);
OleDbCommand comando = new OleDbCommand(consulta, con);
comando.Parameters.Add("@nome", OleDbType.VarChar).Value = txtnome.Text;
comando.Parameters.Add("@nome pet", OleDbType.VarChar).Value = txtnomepet.Text;
comando.Parameters.Add("@cpf", OleDbType.VarChar).Value = txtcpf.Text;
comando.Parameters.Add("@telefone", OleDbType.VarChar).Value = txttelefone.Text;
comando.Parameters.Add("@celular", OleDbType.VarChar).Value = txtcelular.Text;
comando.Parameters.Add("@endereco", OleDbType.VarChar).Value = txtendereco.Text;
try
{
con.Open();
comando.ExecuteNonQuery();
}
catch()
{
}
finally
{
}
}
private void btnpesquisar_Click(object sender, EventArgs e)
{
string conexao = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source="+ Application.StartupPath +@"\Cadastro de Clientes.accdb";
string consulta = "select * from Clientes where nome = @nome";
OleDbConnection con = new OleDbConnection(conexao);
OleDbCommand comando = new OleDbCommand(consulta, con);
comando.Parameters.Add("@nome", OleDbType.VarChar).Value = txtpesquisanome.Text;
try
{
con.Open();
OleDbDataReader oledr = comando.ExecuteReader();
if (oledr.HasRows == false)
{
throw new Exception("Cliente não encontrado...");
}
else
{
oledr.Read();
txtnome.Text = Convert.ToString(oledr["nome"]);
txtnomepet.Text = Convert.ToString(oledr["nome pet"]);
txtcpf.Text = Convert.ToString(oledr["cpf"]);
txttelefone.Text = Convert.ToString(oledr["telefone"]);
txtcelular.Text = Convert.ToString(oledr["celular"]);
txtendereco.Text = Convert.ToString(oledr["endereço"]);
}
}
catch (Exception EX)
{
MessageBox.Show(EX.Message);
}
finally
{
con.Close();
}
Se alguém puder me ajudar, agradeço!