1 pièce(s) jointe(s)
C# windows forms insérer données textbox dans table accdb VS2017
Bonjour,
Qui peut m'aider à faire fonctionner ce fichier en c# VS2017....
Je tiens à préciser que je souhaite dans un premier utiliser uniquement une base ACCESS sans DataSet (uniquement bdd access)... ENFIN, SI C'EST POSSIBLE ?
Je veux comprendre et faire en sorte que ça fonctionne pour le principe...
Allez comprendre pourquoi je n'arrive pas à insérer des éléments dans ma base accdb depuis mon Windows Forms en C#.
Sachant que j'ai déjà étudié ce problème avec une mdf en VB et que pour ce cas j'y arrive... :scarymov:
J'ai choisi de travailler sur des exemples en ligne dont voici le lien https://www.youtube.com/watch?v=njus...?v=njusjZHSUkM
Bien que la personne utilise VS 2010 Express, je n'ai trouvé que ça...
Il y a deux Forms voici le Form1 (pour le Form 1 ça fonctionne bien il prend en compte le login et le mot de passe):
Code:
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
|
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;
namespace AccessLoginApp
{
public partial class Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form1()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\EmployeeInfo.accdb; Persist Security Info=False;";
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
connection.Open();
checKConnection.Text = "Connection Ok";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Erreur " + ex);
}
}
private void Btn_Login_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand
{
Connection = connection,
CommandText = "select * from EmployeeData where Username='" + txt_Username.Text + "' and Password='" + txt_Password.Text + "'"
};
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1; //count++; numéro de ligne de la table
}
if (count == 1)
{
this.Hide();
Form2 f2 = new Form2();
f2.ShowDialog();
}
else
{
checKConnection.Text = "Nom d'utilisateur et/ou mot de passe sont incorrects";
txt_Username.Text = "";
txt_Password.Text = "";
}
connection.Close();
connection.Dispose();
}
}
} |
Voici le Form2 :
Code:
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
|
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;
namespace AccessLoginApp
{
public partial class Form2 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form2()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\EmployeeInfo.accdb;";
}
private void Btn_Save_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand
{
Connection = connection,
CommandText = "INSERT INTO EmployeeData (EID, FirstName, LastName, Pay) VALUES ('" + txt_eid.Text + "','" + txt_fname.Text + "','" + txt_lname.Text + "','" + txt_pay.Text + "')"
};
command.ExecuteNonQuery();
checKConnection.Text = "Données sauvegardées";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Erreur " + ex);
}
}
private void Form2_Load(object sender, EventArgs e)
{
try
{
connection.Open();
checKConnection.Text = "Connection Ok";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Erreur " + ex);
}
}
private void Btn_delete_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand
{
Connection = connection
};
string query = "DELETE from EmployeeData WHERE EID =" + txt_eid.Text + "";
command.CommandText = query;
command.ExecuteNonQuery();
checKConnection.Text = "Données supprimées";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Erreur " + ex);
}
}
}
} |
Je n'ai aucun message d'erreur et j'ai bien comme message ... Données sauvegardées.
Je joints la base complète en VS2017, en espérant que vous puissiez l'ouvrir.
Comment insérer simplement ?
Par avance merci,
Bruno