Q&A

  • mdb를 odbc로 연결했을때..?
MDB를 ODBC로 연결했는데 ODBC를 코딩으로 하고 싶거든요
꼭 그렇게 해야되요
어떤분이 레지스터에 등록하면 된다는데 어떻게 해야될지 모르겠습니다
아시는 분은 꼭 가르쳐주시기 바랍니다
부탁합니다
4  COMMENTS
  • Profile
    구창민 2003.02.13 00:10


    안녕하세요~ 구창민입니다.

    아래는 예전에 퍼둔 '코딩으로 구현하는 ODBC' 란

    내용의 팁이었던 거 같네여.

    보시고 참고 하시면 도움 되실 거 같습니다.

    항상 즐거운 프로그래밍 하시길~~



    unit MainUnit;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Registry, Buttons, DBTables;

    type
      TMainForm = class(TForm)
        GroupBox1: TGroupBox;
        NTLogin: TRadioButton;
        SQLLogin: TRadioButton;
        GroupBox2: TGroupBox;
        Label5: TLabel;
        UseQuote: TCheckBox;
        UseAnsi: TCheckBox;
        GroupBox3: TGroupBox;
        Label6: TLabel;
        Language: TEdit;
        Translate: TCheckBox;
        region: TCheckBox;
        QueryLog: TCheckBox;
        GroupBox4: TGroupBox;
        DatabaseName: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Description: TEdit;
        Label3: TLabel;
        Server: TEdit;
        Label4: TLabel;
        Label7: TLabel;
        LogInID: TEdit;
        DriverCombo: TComboBox;
        Label8: TLabel;
        DriverFile: TEdit;
        MakeBtn: TBitBtn;
        BaseDatabase: TComboBox;
        OdbcLog: TCheckBox;
        QueryLogFile: TEdit;
        OpenBtn1: TSpeedButton;
        OdbcLogFile: TEdit;
        OpenBtn2: TSpeedButton;
        OpenD: TOpenDialog;
        GroupBox5: TGroupBox;
        Memo1: TMemo;
        procedure GetSQLServerDatabase;
        procedure FormCreate(Sender: TObject);
        procedure DriverComboChange(Sender: TObject);
        procedure MakeBtnClick(Sender: TObject);
        procedure QueryLogClick(Sender: TObject);
        procedure OdbcLogClick(Sender: TObject);
        procedure OpenBtn1Click(Sender: TObject);
        procedure OpenBtn2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      MainForm: TMainForm;
      MyReg   : TRegistry;

    implementation

    {$R *.DFM}

    procedure TMainForm.FormCreate(Sender: TObject);
    begin
       MyReg := TRegistry.Create;
       with MyReg do begin
          RootKey := HKEY_LOCAL_MACHINE;
          if OpenKey('SOFTWAREODBCODBCINST.INI',false) then
             GetKeyNames(DriverCombo.Items);

       end;
       MyReg.Free;

       GetSQLServerDatabase;
    //   Session.GetDatabaseNames(BaseDatabase.Items);

    end;

    procedure TMainForm.DriverComboChange(Sender: TObject);
    begin
       if DriverCombo.Text <> 'SQL Server' then begin
          MessageDlg('이 프로그램은 SQL Server ODBC만을 설정할 수 있습니다.',mtError
    ,[mbOK], 0);
          Exit;
       end;

       MyReg := TRegistry.Create;
       with MyReg do begin
          RootKey := HKEY_LOCAL_MACHINE;
          if OpenKey('SOFTWAREODBCODBCINST.INI'+DriverCombo.Text,false) then
             DriverFile.Text := ReadString('Driver');
       end;
       MyReg.Free;
    end;

    procedure TMainForm.GetSQLServerDatabase;
    var
       SR: TSearchRec;
       dir : string;
    begin
       MyReg := TRegistry.Create;
       try
       with MyReg do begin
          RootKey := HKEY_LOCAL_MACHINE;
          if OpenKey('SOFTWAREMicrosoftMSSQLServerSetup',false) then
             dir := ReadString('SQLPath');

          if FindFirst(Dir + 'Data*.mdf', faAnyFile, SR) = 0 then
           repeat
             BaseDatabase.Items.Add(Copy(SR.Name,0,Length(SR.Name)-4));
           until FindNext(SR) <> 0;
           FindClose(SR);
       end;
       finally
          MyReg.Free;
       end;
    end;

    procedure TMainForm.MakeBtnClick(Sender: TObject);
    begin
       if Length(DatabaseName.Text) = 0 then begin
          MessageDlg('데이터베이스 원본이름을 입력하세요',mtError,[mbok], 0);
          Exit;
       end;

       if Length(DriverFile.Text) = 0 then begin
          MessageDlg('드라이버를 선택해 주세요',mtError,[mbok], 0);
          Exit;
       end;

       if Length(BaseDatabase.Text) = 0 then begin
          MessageDlg('기본데이터베이스를 선택해 주세요',mtError,[mbok], 0);
          Exit;
       end;

       try
       MyReg := TRegistry.Create;
       with MyReg do begin
          RootKey := HKEY_LOCAL_MACHINE;
          if OpenKey('SOFTWAREODBCODBC.INIODBC Data Sources', false) then
             WriteString(DatabaseName.Text,'SQL Server');

          if OpenKey('SOFTWAREODBCODBC.INI'+DatabaseName.Text, true) then begin
             // Step 1
             WriteString('Description',Description.Text);
             WriteString('Server',Server.Text);
             WriteString('Driver',DriverFile.Text);

             // Step 2
             if NtLogin.Checked then
                WriteString('Trusted_Connection','Yes');
             WriteString('LastUser',LoginID.Text);

             // Step 3
             WriteString('Database', BaseDatabase.Text);
             if not UseQuote.Checked then
                WriteString('Quotedld','No');
             if not UseAnsi.Checked then
                WriteString('AnsiNPW','No');

             // Step 4
             WriteString('Language',Language.Text);
             if not Translate.Checked then
                WriteString('Auto Translate','No');
             if Region.Checked then
                WriteString('Regional','Yes');
             if QueryLog.Checked then begin
                WriteString('QueryLog_On','Yes');
                WriteString('QueryLogFile',QueryLogFile.Text);
             end;
             if OdbcLog.Checked then begin
                WriteString('StatsLog_On','Yes');
                WriteString('StatsLogFile',ODBCLogFile.Text);
             end;
          end;
       end;
       MyReg.Free;
          MessageDlg('[ '+DatabaseName.Text+' ] ODBC 설정을 성공적으로 마쳤습니다.',
    mtInformation,[mbOk],0);
       except
          MessageDlg('[ '+DatabaseName.Text+' ] ODBC 설정에 실패하였습니다.',mtError
    ,[mbOk],0);
       end;
    end;

    procedure TMainForm.QueryLogClick(Sender: TObject);
    begin
       if QueryLog.Checked then begin
          QueryLogFile.Enabled := True;
          OpenBtn1.Enabled := True;
       end
       else begin
          QueryLogFile.Enabled := false;
          OpenBtn1.Enabled := false;
       end;
    end;

    procedure TMainForm.OdbcLogClick(Sender: TObject);
    begin
       if OdbcLog.Checked then begin
          OdbcLogFile.Enabled := True;
          OpenBtn2.Enabled := True;
       end
       else begin
          OdbcLogFile.Enabled := false;
          OpenBtn2.Enabled := false;
       end;
    end;

    procedure TMainForm.OpenBtn1Click(Sender: TObject);
    begin
       if OpenD.Execute then
          QueryLogFile.Text := OpenD.FileName;
    end;

    procedure TMainForm.OpenBtn2Click(Sender: TObject);
    begin
       if OpenD.Execute then
          OdbcLogFile.Text := OpenD.FileName;
    end;

    end.



    ============================================================================
    .dfm


    object MainForm: TMainForm
      Left = 210
      Top = 116
      ActiveControl = DatabaseName
      BorderIcons = [biSystemMenu, biMinimize]
      BorderStyle = bsSingle
      Caption = 'SQL Server ODBC 맹글기'
      ClientHeight = 512
      ClientWidth = 721
      Color = clBtnFace
      Font.Charset = ANSI_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = '굴림체'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      PixelsPerInch = 96
      TextHeight = 12
      object GroupBox1: TGroupBox
        Left = 296
        Top = 16
        Width = 417
        Height = 121
        Caption = '< STEP 2 >'
        TabOrder = 0
        object Label7: TLabel
          Left = 16
          Top = 88
          Width = 54
          Height = 12
          Caption = '로그인 ID'
        end
        object NTLogin: TRadioButton
          Left = 16
          Top = 32
          Width = 313
          Height = 17
          Caption = '네트워크 로그인 ID를 사용한 Windows NT 인증사용'
          Checked = True
          TabOrder = 0
          TabStop = True
        end
        object SQLLogin: TRadioButton
          Left = 16
          Top = 56
          Width = 393
          Height = 17
          Caption = '사용자가 입력한 로그인 ID 및 암호를 사용한 SQL Server 인증사용'

          TabOrder = 1
        end
        object LogInID: TEdit
          Left = 80
          Top = 80
          Width = 225
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          TabOrder = 2
          Text = 'sa'
        end
      end
      object GroupBox2: TGroupBox
        Left = 296
        Top = 144
        Width = 417
        Height = 121
        Caption = '< STEP 3 >'
        TabOrder = 1
        object Label5: TLabel
          Left = 16
          Top = 21
          Width = 138
          Height = 12
          Caption = '사용할 기본데이터베이스'
        end
        object UseQuote: TCheckBox
          Left = 16
          Top = 64
          Width = 345
          Height = 17
          Caption = '따옴표 붙은 ANSI 식별자 사용'
          TabOrder = 0
        end
        object UseAnsi: TCheckBox
          Left = 16
          Top = 80
          Width = 273
          Height = 17
          Caption = 'ANSI 널, 채움문자 및 경고사용'
          TabOrder = 1
        end
        object BaseDatabase: TComboBox
          Left = 16
          Top = 40
          Width = 289
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          ItemHeight = 12
          TabOrder = 2
        end
      end
      object GroupBox3: TGroupBox
        Left = 8
        Top = 272
        Width = 337
        Height = 201
        Caption = '< STEP 4 >'
        TabOrder = 2
        object Label6: TLabel
          Left = 16
          Top = 16
          Width = 24
          Height = 12
          Caption = '언어'
        end
        object OpenBtn1: TSpeedButton
          Left = 304
          Top = 112
          Width = 23
          Height = 22
          Enabled = False
          Glyph.Data = {
            76010000424D7601000000000000760000002800000020000000100000000100
            04000000000000010000120B0000120B00001000000000000000000000000000
            800000800000008080008000000080008000808000007F7F7F00BFBFBF000000
            FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00555555555555
            555555FFFFFFFFFF55555000000000055555577777777775FFFF00B8B8B8B8B0
            0000775F5555555777770B0B8B8B8B8B0FF07F75F555555575F70FB0B8B8B8B8
            B0F07F575FFFFFFFF7F70BFB0000000000F07F557777777777570FBFBF0FFFFF
            FFF07F55557F5FFFFFF70BFBFB0F000000F07F55557F777777570FBFBF0FFFFF
            FFF075F5557F5FFFFFF750FBFB0F000000F0575FFF7F777777575700000FFFFF
            FFF05577777F5FF55FF75555550F00FF00005555557F775577775555550FFFFF
            0F055555557F55557F755555550FFFFF00555555557FFFFF7755555555000000
            0555555555777777755555555555555555555555555555555555}
          NumGlyphs = 2
          OnClick = OpenBtn1Click
        end
        object OpenBtn2: TSpeedButton
          Left = 304
          Top = 168
          Width = 23
          Height = 22
          Enabled = False
          Glyph.Data = {
            76010000424D7601000000000000760000002800000020000000100000000100
            04000000000000010000120B0000120B00001000000000000000000000000000
            800000800000008080008000000080008000808000007F7F7F00BFBFBF000000
            FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF00555555555555
            555555FFFFFFFFFF55555000000000055555577777777775FFFF00B8B8B8B8B0
            0000775F5555555777770B0B8B8B8B8B0FF07F75F555555575F70FB0B8B8B8B8
            B0F07F575FFFFFFFF7F70BFB0000000000F07F557777777777570FBFBF0FFFFF
            FFF07F55557F5FFFFFF70BFBFB0F000000F07F55557F777777570FBFBF0FFFFF
            FFF075F5557F5FFFFFF750FBFB0F000000F0575FFF7F777777575700000FFFFF
            FFF05577777F5FF55FF75555550F00FF00005555557F775577775555550FFFFF
            0F055555557F55557F755555550FFFFF00555555557FFFFF7755555555000000
            0555555555777777755555555555555555555555555555555555}
          NumGlyphs = 2
          OnClick = OpenBtn2Click
        end
        object Language: TEdit
          Left = 16
          Top = 32
          Width = 289
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          TabOrder = 0
          Text = 'us_english'
        end
        object Translate: TCheckBox
          Left = 16
          Top = 64
          Width = 233
          Height = 17
          Caption = '문자 데이터에 대한 변환 실행'
          TabOrder = 1
        end
        object region: TCheckBox
          Left = 16
          Top = 80
          Width = 313
          Height = 17
          Caption = '통화, 숫자, 날짜 및 시간 표기에 국가별 설정 사용'
          TabOrder = 2
        end
        object QueryLog: TCheckBox
          Left = 16
          Top = 96
          Width = 313
          Height = 17
          Caption = '장기 실행 쿼리를 다음 로그파일에 저장'
          TabOrder = 3
          OnClick = QueryLogClick
        end
        object OdbcLog: TCheckBox
          Left = 16
          Top = 152
          Width = 305
          Height = 17
          Caption = 'ODBC 드라이버 통계를 다음 로그파일에 기록'
          TabOrder = 4
          OnClick = OdbcLogClick
        end
        object QueryLogFile: TEdit
          Left = 32
          Top = 112
          Width = 273
          Height = 20
          Enabled = False
          ImeName = '한국어(한글) (MS-IME98)'
          ReadOnly = True
          TabOrder = 5
        end
        object OdbcLogFile: TEdit
          Left = 32
          Top = 168
          Width = 273
          Height = 20
          Enabled = False
          ImeName = '한국어(한글) (MS-IME98)'
          ReadOnly = True
          TabOrder = 6
        end
      end
      object GroupBox4: TGroupBox
        Left = 8
        Top = 16
        Width = 281
        Height = 249
        Caption = '< STEP 1 >'
        TabOrder = 3
        object Label1: TLabel
          Left = 16
          Top = 16
          Width = 126
          Height = 12
          Caption = '데이터베이스 원본이름'
        end
        object Label2: TLabel
          Left = 16
          Top = 64
          Width = 48
          Height = 12
          Caption = '원본설명'
        end
        object Label3: TLabel
          Left = 16
          Top = 112
          Width = 102
          Height = 12
          Caption = '연결할 SQL Server'
        end
        object Label4: TLabel
          Left = 16
          Top = 160
          Width = 48
          Height = 12
          Caption = '드라이버'
        end
        object Label8: TLabel
          Left = 16
          Top = 200
          Width = 78
          Height = 12
          Caption = '드라이버 파일'
        end
        object DatabaseName: TEdit
          Left = 16
          Top = 32
          Width = 257
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          TabOrder = 0
        end
        object Description: TEdit
          Left = 16
          Top = 80
          Width = 257
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          TabOrder = 1
        end
        object Server: TEdit
          Left = 16
          Top = 128
          Width = 257
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          TabOrder = 2
          Text = '(local)'
        end
        object DriverCombo: TComboBox
          Left = 16
          Top = 176
          Width = 257
          Height = 20
          DropDownCount = 15
          ImeName = '한국어(한글) (MS-IME98)'
          ItemHeight = 12
          TabOrder = 3
          OnChange = DriverComboChange
        end
        object DriverFile: TEdit
          Left = 16
          Top = 216
          Width = 257
          Height = 20
          ImeName = '한국어(한글) (MS-IME98)'
          ReadOnly = True
          TabOrder = 4
        end
      end
      object MakeBtn: TBitBtn
        Left = 584
        Top = 480
        Width = 129
        Height = 25
        Caption = '맹 글 기'
        TabOrder = 4
        OnClick = MakeBtnClick
        Kind = bkOK
      end
      object GroupBox5: TGroupBox
        Left = 352
        Top = 272
        Width = 361
        Height = 201
        Caption = '< 알림 >'
        TabOrder = 5
        object Memo1: TMemo
          Left = 16
          Top = 24
          Width = 329
          Height = 169
          BorderStyle = bsNone
          Color = clBtnFace
          ImeName = '한국어(한글) (MS-IME98)'
          Lines.Strings = (
            '이 예제는 SQL Server ODBC 설정을 코딩으로 구현한 것입'
            '니다.'
            ''
            '모든 옵션이 다 들어가 있는 것이 아니며 제가 알아낸'
            '부분에 대한 설정입니다. 모든 부분의 예외처리를 하진 못'
            '했습니다.'
            ''
            '설정이 잘못되었을 경우 [제어판]-[ODBC]에서 생성된'
            'ODBC를 삭제해 주시면 됩니다.'
            ''
            '의문사항이나 개선사항은 아래로 연락주세요.'
            ''
            'parkhs98@netsgo.com'
            'http://myhome.netsgo.com/parkhs98')
          TabOrder = 0
        end
      end
      object OpenD: TOpenDialog
        DefaultExt = 'log'
        Filter = 'Log File (*.log)|*.log'
        Title = '로그파일'
        Left = 664
        Top = 208
      end
    end
  • Profile
    델사랑 2003.02.13 00:28
    답변정말 감사합니다
    그런데 SQLSERVER만 되나요?
    전 MDB를 연결해야되는데요
    MDB를 연결할려면 어떻게 해야되나요?

  • Profile
    구창민 2003.02.13 00:53
    참고하셔서 하시라고 올려드린겁니다.

    맞춤 정보를 제가 알고 있었다면 그걸 올려드렸겟지요 ^_^;

    대충 보아하니 MakeBtnClick 의 내용이 골자같은데

    MDB 에 맞게 수정 해보시면 될거 같은데요?

    그럼~ 즐거운 프로그래밍 하시구여~



  • Profile
    델사랑 2003.02.13 01:27