连接后台数据库sql server2000。
可以直接对adoconnection控件,通过build..来直接连接。又或者在程序代码中进行动态赋值进行连接。
为此,我测试了一下,不过老是失败,不知为何?
很简单的测试,两个窗口,form1和form2。
在form1上控件布置如下:
------------------------------------------
服务器名:________(edit1)
数据库名:________ (edit2)
连接 退出
------------------------------------------这个上面还包含一个adoconnection控件。
分别填写上面两栏,点连接,正确时候连接上数据库,并且显示form2,否则,提示错误信息。代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
try
ADOConnection1.Close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;'
+'Integrated Security=SSPI;Persist Security Info=false;'
+'Initial Catalog='+edit2.Text+';Data Source='+edit1.Text ;
ADOConnection1.Open;
if ADOConnection1.Connected then
begin
form2.Show;
form1.Hide;
end;
except
application.MessageBox('数据库连接失败,请确认无误后重试!','提示:')
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
application.Terminate ;
end;end.为什么即使我不在那两栏填写任何信息,点“连接”,它都还是自动显示form2?感觉好象没效果??
另外,那段连接代码是否正确?
我是自己使用build..建立连接成功时候看到的,所以赋值就是按按照那样来的?对么。帮下忙,谢谢~~~
可以直接对adoconnection控件,通过build..来直接连接。又或者在程序代码中进行动态赋值进行连接。
为此,我测试了一下,不过老是失败,不知为何?
很简单的测试,两个窗口,form1和form2。
在form1上控件布置如下:
------------------------------------------
服务器名:________(edit1)
数据库名:________ (edit2)
连接 退出
------------------------------------------这个上面还包含一个adoconnection控件。
分别填写上面两栏,点连接,正确时候连接上数据库,并且显示form2,否则,提示错误信息。代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
try
ADOConnection1.Close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;'
+'Integrated Security=SSPI;Persist Security Info=false;'
+'Initial Catalog='+edit2.Text+';Data Source='+edit1.Text ;
ADOConnection1.Open;
if ADOConnection1.Connected then
begin
form2.Show;
form1.Hide;
end;
except
application.MessageBox('数据库连接失败,请确认无误后重试!','提示:')
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
application.Terminate ;
end;end.为什么即使我不在那两栏填写任何信息,点“连接”,它都还是自动显示form2?感觉好象没效果??
另外,那段连接代码是否正确?
我是自己使用build..建立连接成功时候看到的,所以赋值就是按按照那样来的?对么。帮下忙,谢谢~~~
试下加多个判断条件:
if (ADOConnection1.Connected)and(Edit1.Text<>'')and(Edit2.Text<>'') then
begin
form2.Show;
form1.Hide;
end;
再将Connected设成True,就会发现ConnectionString会自动添加了一些东西,DefaultDatabase也会改变。所以服务器名和数据库名不能同时为空
刚检查了下,adoconnection的那个控件我使用build...手动创建过连接,没有删除,所以,一直都可以连上去,代码就没有效果了。现在我删除了那,只使用代码,怎么还上不行?这段连接代码:begin
try
ADOConnection1.Close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;'
+'Integrated Security=SSPI;Persist Security Info=false;'
+'Initial Catalog='+edit2.Text+';Data Source='+edit1.Text ;
ADOConnection1.Open;
...........
到底有没有问题哈?是不是掉了符号什么的??
头大了..
代码中 Integrated Security=SSPI 是不是就是这个意思吧..
我是看一个参考书这样写的代码?中间有没有掉冒号分号什么的?....
有人来冒泡指导下吗?
ADOConnection1.Close;
// calling EditConnectionString displays the dialog, and
// sets the ConnectString property to the resulting value.
if EditConnectionString(ADOConnection1) then begin
//display the resulting ADO Connect String, just for fun.
label11.caption := ADOConnection1.ConnectionString;
end;