« 仕事に役立つ素材集 | メイン | SONY 新BDレコーダー »

2007年9月12日

ODBC設定の自動化

odbcconfコマンドを使用すると、バッチファイル等から設定できるようです。

参考サイト:patagonの日記

VB/Cから実行するにはSQLConfigDataSource()APIを使用します。

・VBサンプル(Windows認証の場合)

Private Const ODBC_ADD_DSN = 1           ' Add a new data source.
Private Const ODBC_CONFIG_DSN = 2        ' Configure (edit) existing data source.
Private Const ODBC_REMOVE_DSN = 3        ' Remove existing data source.
Private Const ODBC_ADD_SYS_DSN = 4       ' Add data source
Private Const ODBC_CONFIG_SYS_DSN = 5    ' Configure (edit) data source
Private Const ODBC_REMOVE_SYS_DSN = 6    ' Remove data source

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
   hwndParent As Long, ByVal fRequest As Long, ByVal _
   lpszDriver As String, ByVal lpszAttributes As String) As Long

Public Function CreateDSN(p_strServerName As String) As Long

    Dim ret As Long
    Dim Driver As String
    Dim Attributes As String
   
    Driver = "SQL Server" & Chr(0)
    Attributes = "DSN=" & g_strDatabaseName & Chr(0)
    Attributes = Attributes & "Database=" & g_strDatabaseName & Chr(0)
    Attributes = Attributes & "Server=" & g_strServerName & Chr(0)
    Attributes = Attributes & "Trusted_Connection=Yes" & Chr(0)
   
    ' 削除
    ret = SQLConfigDataSource(0, ODBC_REMOVE_DSN, Driver, Attributes)
    ' 追加
    ret = SQLConfigDataSource(0, ODBC_ADD_DSN, Driver, Attributes)
  
    CreateDSN = ret

End Function

サンプルはSQLServer用ですが、他のDBでも同じように実行できると思います。パラメータは違うでしょうが・・・。

↓クリックして頂けるとうれしいです(ワンクリック詐欺ではないです(笑))
banner_03人気blogランキングへ
にほんブログ村 地域生活ブログ 群馬情報へ にほんブログ村 サッカーブログ 少年サッカーへ にほんブログ村 IT技術ブログへ 日本ブログ村へ

投稿者 danna : 2007年9月12日 03:00

トラックバック

このエントリーのトラックバックURL:
http://www.namibuta.net/cgi-bin/mt/mt-tb.cgi/716

コメント

コメントしてください




保存しますか?