Comment connecter C# a Microsoft Aceess 2010

bayans Messages postés 14 Date d'inscription mardi 17 novembre 2009 Statut Membre Dernière intervention 2 décembre 2012 - 2 déc. 2012 à 15:33
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 - 5 déc. 2012 à 10:30
J'ai ecris un code sur C# et depuis plusieurs jours je n'arrive pas a le connecter avec Access 2010 et lorsque j'ai essayer le programme cela m'a donne des erreurs que j'ai du mal a comprendre. J'ai besoin d'aide Ça ne fait que quelque semaines que j'étudie C# et Access 2010.

Ceci est ce que j'ai ecris sur C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace Admin
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\storage-stud\user-area\1\....";
OleDbConnection myConnection = new OleDbConnection(connString);

string myQuery = "INSERT INTO ParentTable(Name, Surname, Address, Postcode, Telephone number, Email, Username, Password) VALUES ( '" + TextBox1.Text + "' , '" + TextBox2.Text + "' , '" + TextBox3.Text + "','" + TextBox4.Text + "' , '" + TextBox5.Text + "' , '" + TextBox6.Text + "' , '"+ TextBox7.Text + "' , '" + TextBox8.Text + "')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
Label1.Text = "successful registration";
}
catch (Exception ex)
{
Label1.Text = "Exception in DBHandler" + ex;
}
finally
{
myConnection.Close();
}


}


protected void Button2_Click(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\storage-stud\user-area\1\tj134\2nd year\App & web dev\CW\Microsoft Access\Registration.accdb"; // put your path
OleDbConnection myConnection = new OleDbConnection(connString);

string myQuery = "INSERT INTO ChildTable(First name, DOB, Gender, Username, Password) VALUES ( '" + TextBox9.Text + "' ,'" + DateTime.Now.Date + "' , '" + TextBox10.Text + "' , '" + TextBox11.Text + "','" + DropDownList + "')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
Label2.Text = "successful registration";
}
catch (Exception ex)
{
Label2.Text = "Exception in DBHandler" + ex;
}
finally
{
myConnection.Close();
}

}

}
}

En essayant de cliquer de confirmer mes donnees voila l'erreur que je ne comprend pas du tout.


Exception in DBHandlerSystem.Data.OleDb.OleDbException (0x80004005): 'D:\storage-stud\user-area\1\.....' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides. at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at Admin._Default.Button1_Click(Object sender, EventArgs e)

2 réponses

Déja un petit conseille fait une requete Sql dans ton programme pour affiche ta BDD dans une datagridview. Si la bdd ne s'affiche pas c est qu il y a un probleme .

Je pense que ton probleme vient de ta connexion .
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\storage-stud\user-area\1\tj134\2nd year\App & web dev\CW\Microsoft Access\Registration.accdb"; 

Essaye peut etre de faire une chemin absolue pour aller cherche ta BDD .
0
krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
5 déc. 2012 à 10:30
Salut,

"D:\storage-stud\user-area\1\.....' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."
Traduction en français : "D:\storage-stud\user-area\1\.....' n'est pas un chemin valide. Vérifiez que vous l'avez correctement écrit et que vous êtes connecté au serveur sur lequel le fichier se trouve.

Ça me semble clair : ton chemin d'accès est erroné.

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\storage-stud\user-area\1\tj134\2nd year\App & web dev\CW\Microsoft Access\Registration.accdb";


Déjà, le @ en début de chaine indique que la chaine est verbatime. Donc on ne doit pas doubler les '\'. Donc remplace D:\\ par D:\
Ensuite, un nom de dossier avec un '&', tu cherches les emmerdes...

Et bien sûr, vérifie que tu ne t'es planté nullepart dans le chemin d'accès.

Krimog : while (!(succeed = try())) ;
- Nous ne sommes pas des décodeurs ambulants. Le style SMS est prohibé. -
0
Rejoignez-nous