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

De Banane Atomic
Aller à la navigationAller à la recherche
m (a déplacé CSharp et ODBC vers ODBC et CSharp)
 
Aucun résumé des modifications
 
(7 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
{|
[[Category:CSharp]]
!style="padding: 0 10px 0 0"| [[File:csharp.png|40px]]
= Connection and query =
|
Nuget package: {{boxx|System.Data.Odbc}}
<syntaxhighlight lang="csharp">
<kode lang="csharp">
OdbcDataAdapter dbAdaptater = new OdbcDataAdapter("SELECT * FROM table WHERE validity_date < ?", odbcConnection);
// use an ODBC DSN name
using var odbcConnection = new OdbcConnection("DSN=MyConnection");


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


var dt = new DataTable();
var dataTable = new DataTable();
odbcConnection.Open();
odbcConnection.Open();
dbAdaptater.Fill(dt);
odbcDataAdapter.Fill(dataTable);
odbcConnection.Close();
 
</syntaxhighlight>
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>
 
= Get the table names =
<kode lang='cs'>
var tableNames = odbcConnection.GetSchema("Tables")
    .AsEnumerable()
    .Where(r => r.Field<string>("TABLE_TYPE") == "TABLE")
    .Select(x => x.ItemArray[2]);


[[Category:CSharp]]
Console.WriteLine(string.Join("\n", tableNames));
</kode>

Dernière version du 13 mars 2023 à 15:39

Connection and query

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)));

Get the table names

Cs.svg
var tableNames = odbcConnection.GetSchema("Tables")
    .AsEnumerable()
    .Where(r => r.Field<string>("TABLE_TYPE") == "TABLE")
    .Select(x => x.ItemArray[2]);

Console.WriteLine(string.Join("\n", tableNames));