Exception

Levotre123 - 24 oct. 2013 à 12:14
Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 - 24 oct. 2013 à 20:20
Bonjour les amis

Je suis en développement d'une application d'importation d'un fichier texte vers une BD excel. Mais seulement lors que je fais appel à la fonction d'importation du dit fichier vers un tableau excel je reçois un message du genre :
Le moteur de base de données Microsoft Jet ne peut pas ouvrir il est déjà ouvert en mode exclusif par un autre utilisateur ; ou vous devez avoir l'autorisation de visualiser les données .
Or je suis le seul utilisateur ayant comme privilège Admin et le fichier est bel et bien ferme qui peut avoir une idée sur le sujet
En voici le code source

private void Importerbutton_Click(object sender, EventArgs e)
{
try
{
//delete1();
//delete2();
//droptable();
//droptable1();
DialogResult result;
result = MessageBox.Show("En important les données, vous allez écraser les données existantes. voulez vous vraiment importer ce fichier?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == System.Windows.Forms.DialogResult.Yes)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbCommand myCommand1 = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbCommand myCommand2 = new System.Data.OleDb.OleDbCommand();
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\\Program Files\\Rototype\\Rotokiosk\\Resources\\Dolphin Database.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
myCommand1.Connection = MyConnection;
myCommand2.Connection = MyConnection;


int rowscount = dataGridView1.RowCount;

for (int i = 0; i < rowscount - 1; i++)
{
string selectbck = "select * from [Backup$] where evenement='" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "'";
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(selectbck, MyConnection);
DataSet ds = new DataSet();
da.Fill(ds, "[Backup$]");

int returned = ds.Tables["[Backup$]"].Rows.Count;
if (returned == 0)
{
string value1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
string value2 = dataGridView1.Rows[i].Cells[0].Value.ToString();
string value3 = dataGridView1.Rows[i].Cells[1].Value.ToString();
string value4 = dataGridView1.Rows[i].Cells[6].Value.ToString();
string value5 = dataGridView1.Rows[i].Cells[3].Value.ToString();
string value6 = dataGridView1.Rows[i].Cells[4].Value.ToString();
string value7 = dataGridView1.Rows[i].Cells[8].Value.ToString();
string value8 = dataGridView1.Rows[i].Cells[2].Value.ToString();
int id = i + 1;
string sql = "Insert into [Users$] (ID,CardCode,Name,Category,BankNumber,BranchNumber,Custom1,Custom2,Custom3,Custom4,CustomBitmap,Pin) values('" + id + "','cjd','" + value1 + "','User','" + value2 + "','" + value3 + "','" + value4 + "','" + value5 + "','" + value6 + "','" + value7 + "','rototype.bmp','1234')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
//string value8 = dataGridView1.Rows[i].Cells[2].Value.ToString();
string sql1 = "insert into [AccountNumbers$] (ID,[Number],UserID) values('" + id + "','" + value8 + "','" + id + "')";
myCommand1.CommandText = sql1;
myCommand1.ExecuteNonQuery();
}
else
{

}

}
MessageBox.Show("Le fichier est importé avec succes.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
MyConnection.Close();
backup();
dataGridView1.DataSource = null;

}
}//TRY
catch (Exception don)
{
//MessageBox.Show(don.ToString());
MessageBox.Show(don.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
            
                

1 réponse

Whismeril Messages postés 19024 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 18 avril 2024 656
24 oct. 2013 à 20:20
Bonjour, je n'utilise pas excel comme ça, mais je pense qu'ici:
                    myCommand.Connection = MyConnection;
myCommand1.Connection = MyConnection;
myCommand2.Connection = MyConnection;


Tu crées 3 connexions et donc tu es toi même 3 utilisateurs.
2
Rejoignez-nous