{"id":68,"date":"2007-05-11T21:24:58","date_gmt":"2007-05-11T19:24:58","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/adding-odbc-treiberdsn-with-delphi\/"},"modified":"2007-05-11T21:24:59","modified_gmt":"2007-05-11T19:24:59","slug":"adding-odbc-treiberdsn-with-delphi","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/adding-odbc-treiberdsn-with-delphi\/","title":{"rendered":"Adding ODBC-Treiber\/DSN with Delphi"},"content":{"rendered":"<p><em>> Hi!<br \/>\n><br \/>\n> I have to check if some ODBC data source is created and if not create<br \/>\n> it in runtime. APIs for this are in odbc32.dll but there seem to be no<br \/>\n> help nor function prototypes for that in Delphi. If someone know&#8217;s how<br \/>\n> to do that or have done it already I would appreciate to get some code<br \/>\n> example and odbc32.dll interface unit.<br \/>\n><br \/>\n> tomi.<\/em><\/p>\n<p>A simple example I seem to re-post regularly is as follows.<br \/>\nFor non-MSAccess databases you only need a single call<br \/>\nand can ignore the DBQ stuff.<\/p>\n<p>For future reference, you&#8217;d probably get more\/better response<br \/>\nin the borland.public.delphi.database.desktop ng. to this sort<br \/>\nof question&#8230;<\/p>\n<p>HTH.<\/p>\n<p>[code lang=&#8221;delphi&#8221;](* NOTE: THIS EXAMPLE IS NOT COMPLETE !!! *)<\/p>\n<p>(*<br \/>\n  This example shows one way to load the ODBC<br \/>\n  Administrator&#8217;s DLL (ODBCCP32.DLL) to create<br \/>\n  an Access MDB file and ODBC DSN pointing at<br \/>\n  it.  Note that it assumes current directory<br \/>\n  for both the DLL and the MDB, but the DLL<br \/>\n  will be found if in the WinSys directory which<br \/>\n  is where it normally is anyway.<\/p>\n<p>  Similar operation applies to most driver types,<br \/>\n  with some modifications. eg: Access requires<br \/>\n  the MDB file to exist so you can hook the DSN<br \/>\n  to it.<\/p>\n<p>  Note also that the &#8220;CREATE_DB&#8221; call is an Access<br \/>\n  special (MS Jet Engine) and has other variants<br \/>\n  like COMPACT_DB and REPAIR_DB. For a full list<br \/>\n  see either the Jet Engine Programmers Guide or<br \/>\n  the MSDN and search for &#8220;CREATE_DB&#8221;.<\/p>\n<p>  This was originally written in MSVC6 and was<br \/>\n  ported to Delphi 5.<\/p>\n<p>  Full documentation can be found in the MSDN<br \/>\n  at http:\/\/msdn.microsoft.com\/.<br \/>\n*)<\/p>\n<p>const<br \/>\n  ODBC_ADD_DSN        = 1;    \/\/ Add data source<br \/>\n  ODBC_CONFIG_DSN     = 2;    \/\/ Configure (edit) data source<br \/>\n  ODBC_REMOVE_DSN     = 3;    \/\/ Remove data source<br \/>\n  ODBC_ADD_SYS_DSN    = 4;    \/\/ add a system DSN<br \/>\n  ODBC_CONFIG_SYS_DSN = 5;    \/\/ Configure a system DSN<br \/>\n  ODBC_REMOVE_SYS_DSN = 6;    \/\/ remove a system DSN<\/p>\n<p>type<br \/>\n  TSQLConfigDataSource = function( hwndParent: HWND;<br \/>\n                                   fRequest: WORD;<br \/>\n                                   lpszDriver: LPCSTR;<br \/>\n                                   lpszAttributes: LPCSTR ) : BOOL; stdcall;<\/p>\n<p>procedure Form1.FormCreate(Sender: TObject);<br \/>\nvar<br \/>\n  pFn: TSQLConfigDataSource;<br \/>\n  hLib: LongWord;<br \/>\n  strDriver: string;<br \/>\n  strHome: string;<br \/>\n  strAttr: string;<br \/>\n  strFile: string;<br \/>\n  fResult: BOOL;<br \/>\n  ModName: array[0..MAX_PATH] of Char;<br \/>\n  srInfo : TSearchRec;<br \/>\n  hRegLib: HMODULE;<br \/>\nbegin<br \/>\n  Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );<br \/>\n  strHome := ModName;<br \/>\n  while ( strHome[length(strHome)] <> &#8216;\\&#8217; ) do<br \/>\n    Delete( strHome, length(strHome), 1 );<br \/>\n  strFile := strHome + &#8216;TestData.MDB&#8217;;   \/\/ Test Access Rights (Axes =<br \/>\nAccess)<br \/>\n  hLib := LoadLibrary( &#8216;ODBCCP32&#8217; );    \/\/ load from default path<br \/>\n  if( hLib <> NULL ) then<br \/>\n  begin<br \/>\n    @pFn := GetProcAddress( hLib, &#8216;SQLConfigDataSource&#8217; );<br \/>\n    if( @pFn <> nil ) then<br \/>\n    begin<br \/>\n      \/\/ force (re-)create DSN<br \/>\n      strDriver := &#8216;Microsoft Access Driver (*.mdb)&#8217;;<br \/>\n      strAttr := Format( &#8216;DSN=TestDSN&#8217;+#0+<br \/>\n                         &#8216;DBQ=%s&#8217;+#0+<br \/>\n                         &#8216;Exclusive=1&#8217;+#0+<br \/>\n                         &#8216;Description=Test Data&#8217;+#0+#0,<br \/>\n                         [strFile] );<br \/>\n      fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );<br \/>\n      if( fResult = false ) then ShowMessage( &#8216;Create DSN (Datasource)<br \/>\nfailed!&#8217; );<\/p>\n<p>      \/\/ test\/create MDB file associated with DSN<br \/>\n      if( FindFirst( strFile, 0, srInfo ) <> 0 ) then<br \/>\n      begin<br \/>\n        strDriver := &#8216;Microsoft Access Driver (*.mdb)&#8217;;<br \/>\n        strAttr := Format( &#8216;DSN=TestDSN&#8217;+#0+<br \/>\n                           &#8216;DBQ=%s&#8217;+#0+<br \/>\n                           &#8216;Exclusive=1&#8217;+#0+<br \/>\n                           &#8216;Description=Test Data&#8217;+#0+<br \/>\n                           &#8216;CREATE_DB=&#8221;%s&#8221;&#8216;#0+#0,<br \/>\n                           [strFile,strFile] );<br \/>\n        fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );<br \/>\n        if( fResult = false ) then ShowMessage( &#8216;Create MDB (Database file)<br \/>\nfailed!&#8217; );<br \/>\n      end;<br \/>\n      FindClose( srInfo );<\/p>\n<p>      end;<\/p>\n<p>    (* MASSIVE SNIP *)<\/p>\n<p>    FreeLibrary( hLib );<br \/>\n  end<br \/>\n  else<br \/>\n  begin<br \/>\n    ShowMessage( &#8216;Unable to load ODBCCP32.DLL&#8217; );<br \/>\n  end;<br \/>\n  StatusClockTimer.Enabled := true;<br \/>\nend;[\/code]<\/p>\n<p>[tags]Delphi, API, ODBC[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>> Hi! > > I have to check if some ODBC data source is created and if not create > it in runtime.&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[84,75,85],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-16","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/68"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}