このエントリーをはてなブックマークに追加


C#でインストールされているODBCドライバの一覧を取得する

C#で odbccp32.dll の SQLGetInstalledDrivers API を呼び出し、ODBCドライバの一覧を画面に表示するコンソールアプリケーションの記事になります。

動作確認環境

  • Windows 10 バージョン 22H2
  • Visual Studio 2019
  • .NET Framework 4.7.2

参考サイト

C# サンプルコード

C#からodbccp32.dllのSQLGetInstalledDriversを呼び出すサンプルコードになります。

 
using System;
using System.Runtime.InteropServices;

namespace OdbcDriverList
{
    static class Win32OdbcApi
    {
        [DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        internal static extern bool SQLGetInstalledDrivers(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut);
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] odbcDriverNames = null;
            char[] driverNamesBuffer = new char[ushort.MaxValue];

            bool succeeded = Win32OdbcApi.SQLGetInstalledDrivers(
                driverNamesBuffer,
                ushort.MaxValue,
                out ushort size);

            if (succeeded)
            {
                char[] driverNames = new char[size - 1];
                Array.Copy(driverNamesBuffer, driverNames, size - 1);
                odbcDriverNames = (new string(driverNames)).Split('\0');

                Console.WriteLine(String.Join("\n",odbcDriverNames));
            }
            else
            {
                Console.Error.WriteLine("ERROR: SQLGetInstalledDrivers ODBC API");
            }
        }
    }
}

実行結果

上記 C# コンソールアプリケーションの実行結果になります。

01.png
 

以上、C#でODBCドライバの一覧を表示するサンプルコードの紹介でした。


添付ファイル: file01.png 44件 [詳細] fileProgram.cs 67件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-01-22 (日) 17:09:23