CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  VCL组件开发及应用

那位高手能帮我看一下这段代码,为什么总返出同样的值,是不是判断函数出错?

楼主HelpMeNow(混口飯吃)2003-03-04 08:59:54 在 Delphi / VCL组件开发及应用 提问

本人做的一个三层数据库,以下是实现从DB.ini配置文件的读取参数和一个判断数据库是否连接的函数  
  Database:   TDatabase;  
  .....  
  //读取配置文件信息  
  procedure   ReadFromIni(var   aServerName,   aDatabaseName,   aUserName,   aPassword:   string);  
  var  
      IniFile:   TIniFile;  
      FileName:   string;  
  begin  
      FileName   :=   ExtractFilePath(application.ExeName)   +   '\DB.ini';  
      IniFile   :=   TIniFile.Create(FileName);  
      aServerName   :=   IniFile.ReadString('DB',   'ServerName',   '');  
      aDatabaseName   :=   IniFile.ReadString('DB',   'DatabaseName',   '');  
      aUserName   :=   IniFile.ReadString('DB',   'UserName',   '');  
      aPassword   :=   IniFile.ReadString('DB',   'Password',   '');  
      IniFile.Free;  
  end;  
  ..  
  //判断能否连接上数据库的函数  
  function   TfrmMain.DBConnect(aServerName,   aDatabaseName,   aUserName,   aPassword:   string):   boolean;  
  begin  
      Result   :=   True;  
      Database.connected   :=   False;  
      Database.DriverName   :=   'SQL   server';   //驱动名称  
      Database.Params.Values['Server   Name']   :=   aServerName;   //服务器名  
      Database.Params.Values['Database   Name']   :=   aDatabaseName;   //数据库名  
      Database.Params.Values['User   Name']   :=   aUserName;//登录数据库用户名  
      Database.Params.Values['Password']   :=   aPassword;   //相应数据库用户的密码  
      try  
          Database.Connected   :=   True;  
      except  
          result   :=   False;  
      end;  
  end;  
  ....  
  DB.ini内容如下  
  [DB]  
  ServerName=king  
  DatabaseName=wzgl  
  UserName=sa  
  Password=  
  ...另一段代码  
  //写入ini,并测试是否连接上  
  procedure   TfrmDBConnect.btnOKClick(Sender:   TObject);  
  begin  
      writeToIni(edtServerName.Text,   edtDatabaseName.Text,   edtUserName.Text,   edtPassword.Text);  
      if   frmMain.DBConnect(edtServerName.Text,   edtDatabaseName.Text,   edtUserName.Text,   edtPassword.Text)   then  
      begin  
          Application.MessageBox('连接成功',   '提示',   mb_iconInformation   +   mb_defbutton1);  
          Connected   :=   True;  
          Close;  
      end  
      else  
      begin  
          Application.MessageBox('连接失败',   '提示',   mb_iconInformation   +   mb_defbutton1);  
      end;  
  end;  
   
  为什么总是提示'连接失败'? 问题点数:100、回复次数:13Top

1 楼li_zhifu(东北人)回复于 2003-03-04 09:17:46 得分 0

FileName   :=   ExtractFilePath(application.ExeName)   +   '\DB.ini';//这样写的话这个文件就是一个不存在的文件,因为ExtractFilePath返回的是一个以'\'结尾的路径,再加上一个'\DB.ini'就成了类似这样'd:\mydbpath\\DB.ini'Top

2 楼HelpMeNow(混口飯吃)回复于 2003-03-04 09:49:56 得分 0

//向配置文件写入信息  
  procedure   WriteToIni(const   aServerName,   aDatabaseName,   aUserName,   aPassword:   string);  
  var  
      IniFile:   TIniFile;  
      FileName:   string;  
  begin  
      FileName   :=   ExtractFilePath(application.ExeName)   +   '\DB.ini';  
      IniFile   :=   TIniFile.Create(FileName);  
      IniFile.WriteString('DB',   'ServerName',   aServerName);  
      IniFile.WriteString('DB',   'DatabaseName',   aDatabaseName);  
      IniFile.WriteString('DB',   'UserName',   aUserName);  
      IniFile.WriteString('DB',   'Password',   aPassword);  
      IniFile.Free;  
  end;  
  但我这段语句却能实现向DB.ini写入信息?Top

3 楼fcyxhome(冰刀封情)回复于 2003-03-04 09:58:32 得分 20

up上面说的很好,为什么不用inifile类呢  
  unit   Unit2;  
   
  interface  
   
  uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      StdCtrls,   ExtCtrls,inifiles;  
   
  type  
      TForm2   =   class(TForm)  
          Timer1:   TTimer;  
          Label1:   TLabel;  
          procedure   FormCreate(Sender:   TObject);  
          procedure   Timer1Timer(Sender:   TObject);  
          procedure   FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X,  
              Y:   Integer);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
  var  
      Form2:   TForm2;  
      pos:integer;  
      speed:integer;  
      ox,oy:integer;  
      Hascall:boolean;  
  implementation  
   
  {$R   *.DFM}  
      uses     unit1;  
  procedure   TForm2.FormCreate(Sender:   TObject);  
  var  
      textinifile:tinifile;  
      s:set   of   Tfontstyle;  
   
   
  begin  
            form2.WindowState:=wsMaximized;  
            form2.BorderStyle:=bsNone;  
   
            Textinifile:=Tinifile.Create('Mysaver.ini');  
            if     textinifile<>nil   then  
                try  
                    with   textinifile   do  
                        begin  
                          form2.Color:=ReadInteger('ScrClor','BColor',clBlack);  
                            form2.Label1.Caption:=ReadString('ScrSaver','text','delphi');  
                            pos:=ReadInteger('ScrSaver','position',0);  
                            speed:=ReadInteger('ScrSaver','speed',0);  
   
                              form2.Label1.Font.Charset:=Readinteger('ScrFont','Charset',GB2312_CHARSET);  
                            form2.Label1.Font.Name:=Readstring('ScrFont','Name','宋体');  
                            form2.Label1.Font.Color:=readinteger('ScrFont','Color',clRed);  
                            form2.Label1.Font.Size:=ReadInteger('ScrFont','Size',13);  
                            form2.Label1.Font.Height:=Readinteger('ScrFont','Height',11);  
                            s:=[];  
                            if   readinteger('ScrFont','StyleB',0)=1   then   s:=S+[fsBold];  
                            if   readinteger('ScrFont','StyleI',0)=1   then   s:=S+[fsItalic];  
                            if   readinteger('ScrFont','StyleU',0)=1   then   s:=S+[fsUnderline];  
                            if   readinteger('ScrFont','StyleS',0)=1   then   s:=S+[fsStrikeOut];  
                            form2.Label1.Font.Style:=s;  
                            Free;  
                          end;  
                          except  
                              textinifile.free;  
                      end;  
                    form2.Timer1.interval:=50-4*speed;  
                    randomize;  
                    if   pos=0   then   form2.Label1.Top:=round(screen.Height/2)  
                          else   form2.Label1.top:=random(screen.Height-50);  
                    cursor:=crNone;  
                    form2.Label1.cursor:=crNone;  
                         
  end;  
   
  procedure   TForm2.Timer1Timer(Sender:   TObject);  
  begin  
            randomize;  
            Form2.Label1.Left:=label1.Left-3-round(speed/2);  
            if   label1.Left+label1.width<0   then  
                  begin  
                      label1.top:=random(screen.height-50);  
                      label1.Left:=screen.width;  
                      end;  
  end;  
   
  procedure   TForm2.FormMouseMove(Sender:   TObject;   Shift:   TShiftState;   X,  
      Y:   Integer);  
  begin  
          if   not   hascall   then  
          begin  
              ox:=x;  
              oy:=y;  
              hascall:=true;  
              end;  
              if   (x<>ox)or(y<>oy)   then  
              begin  
                    close;  
              end;  
  end;  
   
  end.  
   
   
   
  Top

4 楼killlaoli(菜鸟)回复于 2003-03-04 09:59:11 得分 0

好象不是那里的问题,编译器好象是以非'\'字符判断路径及文件名的,比如说:  
  memo1.Lines.LoadFromFile('d:\\\\\\\f\\\\\\\\\f1.txt');  
  和  
  memo1.Lines.LoadFromFile('d:\f\f1.txt');  
  效果是一样的。Top

5 楼netwolfds()回复于 2003-03-04 10:15:45 得分 0

Database.DriverName   :=   'SQL   server'改为Database.DriverName   :=   'MSSQL'  
  另外给Database.databasename赋值,一个在BDE别名中没有的值Top

6 楼snake_eye(别人都叫我老李)回复于 2003-03-04 10:25:06 得分 0

你的程序我看了一遍,没试过估计可能是  
      Database.DriverName   :=   'SQL   server';   //驱动名称  
      Database.Params.Values['Server   Name']   :=   aServerName;   //服务器名  
      Database.Params.Values['Database   Name']   :=   aDatabaseName;   //数据库名  
      Database.Params.Values['User   Name']   :=   aUserName;//登录数据库用户名  
      Database.Params.Values['Password']   :=   aPassword;   //相应数据库用户的密码  
  这些地方有问题,其他的没什么问题!Top

7 楼HelpMeNow(混口飯吃)回复于 2003-03-04 10:25:27 得分 0

实际我是想要动态配置数据库,不想建立一个BDE别名.Top

8 楼cobi(我是小新)回复于 2003-03-04 10:30:08 得分 0

你要动态配置数据库就不应该使用上面的代码  
  你对database控件所写的代码实际是对它的一个配置,要通过配置好的database控件连接到数据库,必须要通过建立别名才能实现Top

9 楼netwolfds()回复于 2003-03-04 10:32:56 得分 80

你先试试我说的,我已经测试通过了  
  不是要建别名,而是  
  1.database.databasename不能为空,  
  2.如果database.databasename的值在bde的别名中有,其他的参数,系统会自动调bde中的参数  
  请看帮助  
  Use   DatabaseName   to   specify   the   name   of   the   database   to   use   with   a   database   component.   If   DatabaseName   is   the   same   as   an   existing   Borland   Database   Engine   (BDE)   alias,   then   the   AliasName   and   DriverName   properties   need   not   be   set.   If   DatabaseName   does   not   match   an   existing   BDE   alias,   then   either   the   application   must   also   supply   a   valid   alias   in   the   AliasName   property   in   addition   to   the   DatabaseName,   or   it   must   supply   the   DriverName   and   Params   properties.Top

10 楼fancier(OP&&C/C++)回复于 2003-03-04 10:38:54 得分 0

关注Top

11 楼DelUser(探索者)回复于 2003-03-04 10:39:50 得分 0

up   不知道问题Top

12 楼HelpMeNow(混口飯吃)回复于 2003-03-04 11:17:20 得分 0

是这样的,如果不加入这样的代码?  
      Database.DriverName   :=   'SQL   server';   //驱动名称  
      Database.Params.Values['Server   Name']   :=   aServerName;   //服务器名  
      Database.Params.Values['Database   Name']   :=   aDatabaseName;   //数据库名  
      Database.Params.Values['User   Name']   :=   aUserName;//登录数据库用户名  
      Database.Params.Values['Password']   :=   aPassword;   //相应数据库用户的密码  
  而是在DataBase组件中,直接设为Alias   Name为一个已存在的BDE别名(ODBC),  
  Parameter     Overries:  
    user   name=sa  
    password  
  这样,其Connected属性可以设为True;  
  或者去掉上面语句中的  
      Database.DriverName   :=   'SQL   server';   //驱动名称  
  这样编译出来的程序就可以通过,这是为什么?Top

13 楼netwolfds()回复于 2003-03-04 11:25:00 得分 0

在DataBase组件中,直接设为Alias   Name为一个已存在的BDE别名(ODBC),那么DriverName,params等等都不管用了,系统直接调bde别名的设置Top

相关问题

  • 为什么在线程中调用主对话框函数时会出错?看一下我的代码
  • 代码出错,帮忙查看一下
  • 写的函数返回出错,请帮忙看一下~
  • 帮我看一下这段代码。怎么总是出错?
  • 帮我看一下这段代码为什么出错?(100分)
  • 大家看一下这段代码,什么地方出错了
  • 大家帮我看一下我的代码哪里出错了!!!
  • ASP调用ORACLE中包的函数出错,请大家帮我看一下。
  • 看一下这个函数
  • 请帮我看一下,下面的代码,说是连接出错!!

关键词

  • 函数
  • 数据库
  • db
  • database
  • scrfont
  • readinteger
  • aservername
  • inifile
  • readstring
  • adatabasename

得分解答快速导航

  • 帖主:HelpMeNow
  • fcyxhome
  • netwolfds

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo