|
The following code is the code of ado dynamically linking the sql database, but it is not connected, I don't know what the reason is, everyone helps to solve it together. The error is that the exception dialog box is skipped, which says, 'Unable to open the database Book_Data requested during login, login failed'
code show as below:
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 name from logged in user');
DLYH_Q.Open;
if DLYH_Q.RecordCount> 0 then
while not DLYH_Q.Eof do
begin
XM_CB.Items.Add (DLYH_Q.FieldValues ['Name']);
DLYH_Q.Next;
end;
DLYH_Q.Close;
FreeAndNil (DLYH_Q);
XM_CB.ItemIndex: = 0;
end; |
|