Salut à tous.
Je suis en train de me faire la main sur ADO.NET
Voici mon souci.
J'ai une base SQLite contenant une table Profils.
J'essaie de l'afficher dans un DataGridView.
Au moment du "dataAdapter.Fill(table);",
une exception me disant que la connexion à la base est impossible est levée...
Je joins mon code, j'ai surement fait une bourde quelque part...
Merci de votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Data.SQLite; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Bankroller { public partial class BankrollerMain : Form { public DbConnection cnn; public BankrollerMain() { InitializeComponent(); DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite"); cnn = fact.CreateConnection(); cnn.ConnectionString = "Data Source=br.db"; cnn.Open(); // Bind the DataGridView to the BindingSource // and load the data from the database. dataGridView1.DataSource = bindingSource1; GetData("select * from Profils"); } private void GetData(string selectCommand) { try { // Create a new data adapter based on the specified query. SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, cnn.ConnectionString); // Create a command builder to generate SQL update, insert, and // delete commands based on selectCommand. These are used to // update the database. SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); // Populate a new data table and bind it to the BindingSource. DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapter.Fill(table); bindingSource1.DataSource = table; // Resize the DataGridView columns to fit the newly loaded content. dataGridView1.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); } catch (SqlException) { MessageBox.Show("To run this example, replace the value of the " + "connectionString variable with a connection string that is " + "valid for your system."); } } } }
Partager