|
下面的代码是ado动态链接sql数据库的代码,但是就是连接不上,不知道是什么原因,大家帮忙一起解决一下。出现的错误是,跳除异常对话框,上面说,'无法打开登陆中请求的数据库Book_Data,登陆失败'
代码如下:
var
Logintimes:integer=0;
procedure TEnter_F.FormCreate(Sender: TObject);
var
DLYH_Q:TADOQuery;
ADOCommand:TADOCommand;
s,DataPath : string;
begin
Connect:=TADOConnection.Create(nil);
Connect.ConnectionString:='Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Book_Data';
Connect.LoginPrompt:=false;
try
Connect.Connected:=true;
except
ADOCommand:=TADOCommand.Create(nil);
ADOCommand.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False';
DataPath:=ExtractFilePath(Application.ExeName) ;
s:='EXEC sp_attach_db @dbname = N'+char(39)+'Book_Data'+char(39)+','+
'@filename1 = N'+char(39)+DataPath+'Book_Data.MDF'+char(39)+
','+'@filename2 = N'+char(39)+DataPath+'Book_Data.LDF'+char(39);
ADOCommand.CommandText := s;
ADOCommand.Execute();
end;
DLYH_Q:=TADOQuery.Create(nil);
DLYH_Q.Connection:=Connect;
DLYH_Q.SQL.Clear;
DLYH_Q.SQL.Add('select 姓名 from 登录用户');
DLYH_Q.Open;
if DLYH_Q.RecordCount >0 then
while not DLYH_Q.Eof do
begin
XM_CB.Items.Add(DLYH_Q.FieldValues['姓名']);
DLYH_Q.Next;
end;
DLYH_Q.Close;
FreeAndNil(DLYH_Q);
XM_CB.ItemIndex:=0;
end;
|
|