Usuário com melhor resposta
dificuldade com conexão do banco de dados do access com o visual studio,

Pergunta
-
boa tarde gostaria de ajuda para resolver um erro em minha aplicação, não tenho muito conhecimento em programação, aqui em baixo estão o erro e o código da pagina aonde ocorre o erro, desde ja agradeço pela atenção.
esse e o erro que esta acontecendo em minha aplicação:
esse e meu codigo :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace Mdragon_2._0
{
public partial class Acesso_ADMs : Form
{
private OleDbConnection Conn;
private OleDbCommand Cmd;
private OleDbDataAdapter da;
private DataSet ds;
private string bd = "MDragon.mdb";
public string codigo, nome_operador, senha_acesso;
public Int32 v;
public Acesso_ADMs()
{
InitializeComponent();
}
public void logar()
{
//define o dataset
ds = new DataSet();
//cria uma conexo usando a string de conexo
Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\MDragon.mdb");
try
{
//abre a conexao
Conn.Open();
}
catch (System.Exception e)
{
MessageBox.Show(e.Message.ToString());
}
if (Conn.State == ConnectionState.Open)
{
//se a conexo estiver aberta usa uma instruo SQL para selecionar os registros da tabela clientes
da = new OleDbDataAdapter("SELECT * from Funcionarios", Conn);
da.Fill(ds, "Login");
}
}
private void btnVoltar_Click(object sender, EventArgs e)
{
Acesso_Cliente flr = new Acesso_Cliente();
flr.Show();
this.Visible = false;
}
private void btnAcessar_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\MDragon.mdb");
Cmd = new OleDbCommand("SELECT COUNT(*) FROM Funcionarios WHERE Cod_Seguranca = @Cod_Seguranca AND Senha = @Senha", Conn);
da = new OleDbDataAdapter(Cmd);
Cmd.Parameters.AddWithValue("@Cod_Seguranca", SqlDbType.Text).Value = txtCodFuncionario.Text;
Cmd.Parameters.AddWithValue("@Senha", SqlDbType.Text).Value = txtSenha.Text;
conn.Open();
v = int.Parse(Cmd.ExecuteScalar().ToString());
if (v > 0)
{
Pagina_dos_ADMs flr = new Pagina_dos_ADMs();
flr.Show();
this.Visible = false;
}
else
{
MessageBox.Show("Erro ao efetuar login");
}
}
}
}
Respostas
-
private void btnAcessar_Click(object sender, EventArgs e) { string stringConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\MDragon.mdb"; using (OleDbConnection conn = new OleDbConnection(stringConnection)) { string qry = "SELECT COUNT(*) FROM Funcionarios WHERE Cod_Seguranca = @Cod_Seguranca AND Senha = @Senha"; using (OleDbCommand cmd = new OleDbCommand(qry, conn)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@Cod_Seguranca", txtCodFuncionario.Text); cmd.Parameters.AddWithValue("@Senha", txtSenha.Text); try { conn.Open(); object val = cmd.ExecuteScalar(); if (!(val is System.DBNull) || val != null) v = (int)val; } catch (OleDbException ex) { string errorMessages = ""; for (int i = 0; i < ex.Errors.Count; i++) { errorMessages += "Index #" + i + "\n" + "Message: " + e.Errors[i].Message + "\n" + "NativeError: " + e.Errors[i].NativeError + "\n" + "Source: " + e.Errors[i].Source + "\n" + "SQLState: " + e.Errors[i].SQLState + "\n"; } MessageBox.Show(errorMessages, "OleDbException", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch(Exception ex) { MessageBox.Show("Exception: " + ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (v > 0) { Pagina_dos_ADMs flr = new Pagina_dos_ADMs(); flr.Show(); this.Visible = false; } else { MessageBox.Show("Erro ao efetuar login"); } }
Editei seu código, porém não testei.
Vê se com esse aparece alguma mensagem de erro?
Espero ter ajudado.
Att.
Michael
- Marcado como Resposta Robson William SilvaModerator segunda-feira, 17 de outubro de 2016 12:00