我想请问下 如何用代码连接到数据库哦!   我不会哦!  你给我个例子好吗?   拜托了。我使用的是: ADOConnection1  和 ADOQuery1 和 DataSource1 和 DBGrid1 这四个控件。

解决方案 »

  1.   

    with ADOConnection1  do
        begin
          close;
          ConnectionString := 'Provider=SQLOLEDB;Server=' + MasterDBInfo.ServerName
            + ';UID=' + MasterDBInfo.UserName + ';PWD=' + MasterDBInfo.Password
            + ';Database=' + MasterDBInfo.DBName;
    ADOQuery1.connection:=ADOConnection1;
    DataSource1.connection:=ADOQuery1
    DBGrid1 .connection:=DataSource1
    就是这样的
      

  2.   


    function TServerMethods1.OpenADOConn: Boolean;
    begin
      result:=true;
      with ADOConnection1 do
      begin
        Connected:= false;
        Provider:= 'SQLOLEDB.1';    Properties['Data Source'].Value:= '127.0.0.1' ; //服务器名
        Properties['Initial Catalog'].Value:= 'DB_Yourname' ;  //表名
        Properties['User ID'].Value:= 'sa'; //用户名
        Properties['password'].Value:= '123456789'; //密码
        LoginPrompt:= false;
        try
          Connected:= true;
    ADOQuery1.connection:=ADOConnection1;
    DataSource1.connection:=ADOQuery1
    DBGrid1 .connection:=DataSource1
        except
          begin
            // 提示失败神马的
          end;
        end;
      end;
    end;
    上面是连接
    ADOQuery1.close;
    ADOQuery1.SQL.TEXT := 'select * from MYTABLE'
    ADOQuery1.Open;
      

  3.   

    {ServerIP=192.168.7.11\SQL2005
    SQLDBName=HC_sql
    SQLUserID=sa
    SQLPwd=sasa} ini文件内容
       function   Get_Db_Con_Str(FileName:String):String;
       var
         ServerIP,SQLDBName,SQLUserID,SQLUserName,SQLPwd:string;
       begin
         Result:='';
         with TIniFile.Create(FileName) do
         begin
           try
              ServerIP:=ReadString('ServerInfo','ServerIP','');
              SQLDBName:=ReadString('ServerInfo','SQLDBName','');
              SQLUserID:=ReadString('ServerInfo','SQLUserID','');
              SQLPwd:=ReadString('ServerInfo','SQLPwd','');
              SQLUserName:=ReadString('ServerInfo','SQLUserName','');
             finally
               Free;
               end;
               Result:='Provider=SQLOLEDB.1;Password='+SQLpwd+';Persist Security Info=True;User ID='+SQLUserID+';Initial Catalog='+SQLDBName+';Data Source='+ServerIP+';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID='+SQLUserName+';Use Encryption for Data=False;Tag with column collation when possible=False';
         end;
       end; ini_FileName:=ExtractFilePath(Application.ExeName)+'conn.ini';
      if not FileExists(ini_FileName)then
      begin
        Application.MessageBox('数据库文件不存在','信息提示',MB_OK);
        Application.Terminate;
      end;
      if not DirectoryExists(ExtractFilePath(Application.ExeName)+'photop_card') then
      begin
         CreateDirectory(PChar(ExtractFilePath(Application.ExeName)+'photop_card'),nil);
      end;
      if not DirectoryExists(ExtractFilePath(Application.ExeName)+'photop_sp') then
      begin
         CreateDirectory(PChar(ExtractFilePath(Application.ExeName)+'photop_sp'),nil);
      end;
      with con1 do
      con1.ConnectionString:=Get_Db_Con_Str(ini_FileName);
      con1.Connected:=False;
      try
        con1.Connected:=True;
        except
           Application.MessageBox('数据库连接失败请联系管理员','信息提示',mb_ok);
           iniF:=TiniF.Create(Application);
           iniF.ShowModal;
          end;
    使用ini文件配置链接数据库。配置错误弹出配置文件对话框进行修改。