« ODBC et CSharp » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 12 : Ligne 12 :
odbcDataAdapter.Fill(dataTable);
odbcDataAdapter.Fill(dataTable);


Console.WriteLine(string.Join("\n", dataTable.AsEnumerable().Select(x => string.Join(" , ", x.ItemArray)));
Console.WriteLine(string.Join("\t,\t", dataTable.Columns.Cast<DataColumn>().Select(x => x.ColumnName)));
Console.WriteLine(string.Join("\n", dataTable.AsEnumerable().Select(x => string.Join("\t,\t"", x.ItemArray)));
</kode>
</kode>

Version du 13 mars 2023 à 15:30

Nuget package: System.Data.Odbc

Csharp.svg
// use an ODBC DSN name
using var odbcConnection = new OdbcConnection("DSN=MyConnection");

var odbcDataAdapter = new OdbcDataAdapter("SELECT * FROM table WHERE validity_date < ?", odbcConnection);
odbcDataAdapter.SelectCommand.Parameters.Add("@date", OdbcType.DateTime).Value = maxLimitDate;

var dataTable = new DataTable();
odbcConnection.Open();
odbcDataAdapter.Fill(dataTable);

Console.WriteLine(string.Join("\t,\t", dataTable.Columns.Cast<DataColumn>().Select(x => x.ColumnName)));
Console.WriteLine(string.Join("\n", dataTable.AsEnumerable().Select(x => string.Join("\t,\t"", x.ItemArray)));