Hola a todos, hoy os voy a dejar una clase conexion para C# y access.
Si necesitas conectar Access con C#, podéis usar esta clase:
|
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 |
class Conexion { private OleDbConnection conexion; public Conexion() { this.conexion = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb"); } public void conectar() { this.conexion.Open(); } public void cerrar() { this.conexion.Close(); } public String[] getSingleRow(string sql) { OleDbCommand cmd = new OleDbCommand(); cmd.Connection = this.conexion; cmd.CommandText = @sql; OleDbDataReader dr = cmd.ExecuteReader(); String[] valores = new String[dr.FieldCount]; if (dr.Read()) { for (int i = 0; i < valores.Length; i++) { valores[i] = dr.GetValue(i).ToString(); } } return valores; } public DataSet ejecutarQuery(String query) { OleDbDataAdapter adapter = new OleDbDataAdapter(query, this.conexion); DataSet d = new DataSet(); adapter.Fill(d); return d; } public bool ejecutarInstruccion(String instruccion) { OleDbCommand cmd = new OleDbCommand(); cmd.Connection = this.conexion; cmd.CommandText = @instruccion; return cmd.ExecuteNonQuery() > 0; } } |
Espero que os sea de ayuda. Si tenéis dudas, preguntad. Estamos para ayudarte.
Deja una respuesta