Home  >  Article  >  Database  >  delphi 数据库之不用数据源 以access为例(其他未试过)

delphi 数据库之不用数据源 以access为例(其他未试过)

WBOY
WBOYOriginal
2016-06-07 15:31:081104browse

在我做好学生管理系统时,发现要使用本软件必须配置 数据 库 的 数据 源。于是我就想弄个 脱离 数据 源的版本。 构造思想: 650) this.width=650;" src="http://cdn.verydemo.com/upload/2013_06_17/13714209463650.jpg" title="datafile.jpg" /> 在con1的 c

在我做好学生管理系统时,发现要使用本软件必须配置数据数据源。于是我就想弄个脱离数据源的版本。

构造思想:

delphi 数据库之不用数据源 以access为例(其他未试过)

在con1的connectionstring有链接数据文件作为数据源的做法,那么我们可以用数据文件配置数据源。

   于是百度了一下Data Source = 数据源路径。而student数据源在注册表中注册过,student数据源名称等价于路径。

思路:

   第一步:马上想到要获得数据所在的绝对路径。

   第二步:代码编写ConnectionString,Provider要写完整数据提供者的驱动名称(如

<span>Provider=Microsoft.Jet.OleDb.4.0(ACCESS<strong><strong><strong>数据</strong></strong></strong>引擎)</span>

),而Data Source写为绝对路径。


技术关键:

   1.获得程序所在目录,构造绝对路径:

sDBPath = extractFilepath(application.ExeName)+'dataname.mdb';

   2.con1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + sDBPath +';';

步骤:

一:有这么一个界面

delphi 数据库之不用数据源 以access为例(其他未试过)

二、 先修改con1的属性

delphi 数据库之不用数据源 以access为例(其他未试过)


修改adoquery的属性

delphi 数据库之不用数据源 以access为例(其他未试过)

   在form2添加onActivate事件(窗口激活事件)代码如下,  

procedure TForm2.FormActivate(Sender: TObject);
 var
  sDBPath : string;
begin
  sDBPath := extractFilepath(application.ExeName) + 'students.mdb';
  ADOQuery1.Active := false;
  con1.Connected := false;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('select * from admins'); //这句看窗口刚刚建立是否要<strong><strong><strong><strong>数据</strong></strong></strong>库</strong>而定要写不写。
  //User ID=Admin;<strong><strong><strong><strong>数据</strong></strong></strong>库</strong>有用户名的相应的加上
  //填写connectionstring
  con1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + sDBPath +';';
  con1.Connected := true;
  ADOQuery1.Active := true;
end;


这样就完成数据脱离数据源的配置。

样例代码 http://pan.baidu.com/share/link?shareid=187085761&uk=2065228996


lilin9105

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn