Bonjour,
je souhaite créer une application C# capable de me créer et remplir un DataTable.
Voici mon code :
Dans un second temps, je dispose d'une classe contenant mes différentes requêtes.
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 public DataTable GetData(string query) { DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OracleClient"); using (DbConnection conn = factory.CreateConnection()) { try { DbConnectionStringBuilder csb = factory.CreateConnectionStringBuilder(); csb["Data Source"] = @""; csb["User Id"] = @""; csb["Password"] = @""; conn.ConnectionString = csb.ConnectionString; conn.Open(); using (DbCommand cmd = conn.CreateCommand()) { cmd.CommandText = query; using (DataTable dt = new DataTable()) { DbDataAdapter da = factory.CreateDataAdapter(); cmd.CommandType = CommandType.Text; da.SelectCommand = cmd; da.Fill(dt); return dt; } } } catch (Exception ex) { throw new Exception("Error", ex); } finally { if (conn.State != ConnectionState.Closed) conn.Close(); } } }
Je souhaiterais donc pouvoir passer query1 ou/et query2 en paramètre de ma méthode GetData.
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 public class Query { public string SQL; public string query1() { this.SQL = "select * from table1" return this.SQL; } public string query2(string param) { this.SQL = "select * from table1 where id = '" + param + "' "; return this.SQL; }
Mais je ne sais comment faire !
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 public DataTable GetData(query1) { //... }
Merci d'avance pour votre aide !
Partager