一个简单的方法,采用赋值的方法,首先建立一个文本文件,然后读给adoconnect的connectionstring,connected:=true,以后修改文本文件就行了,当然文本文件的内容可以使用连接字符串编辑器的build生成
解决方案 »
- ???DBGridEh最下面加合计栏后,如何去掉中间的网格竖线???
- 程序连ODBC数据源 的问题 非常着急 在线等
- Visual FoxPro数据源如何建立
- 消息和模态窗口
- adoquery1移动纪录触发事件
- 我用cooltrayicon控件时当加上advstringGrid控件后,用showmainform命令报错
- ??? 请问如何使用数组,存取几个值 ???
- 急问:stringgrid
- 如何把ToolBar上的几个按钮中的一个放在ToolBar的最右边?
- 用delphi xe3+RTC做了一个服务端,但运行时,连接人数十几个,数据处理量稍大时,就停止反应,要经过一段时间处理后才能继续
- 还请高手帮我?
- 这个怎么实现?高手一定认为很简单!
function GetConnectionString: string;
var
SYSINI: TINIFile;
ServerName, UserName, Password, InitDB: string;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
ServerName := SYSINI.ReadString('Database', 'ServerName', '');
UserName := SYSINI.ReadString('Database', 'UserName', '');
InitDB := SYSINI.ReadString('Database', 'InitDB', '');
tmpstr := SYSINI.ReadString('Database', 'Password', '');
Password := Decrypt(tmpstr, Key);
Result := '';
Result := 'Provider=SQLOLEDB.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Initial Catalog=' + InitDB + ';Data Source=' + ServerName;
finally
SYSINI.Free;
end;
end;procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);
var
SYSINI: TINIFile;
tmpstr: string;
begin
SYSINI := TIniFile.Create('DB.INI');
try
with SYSINI do
begin
WriteString('Database', 'ServerName', ServerName);
WriteString('Database', 'UserName', UserName);
WriteString('Database', 'InitDB', InitDB);
tmpstr := Encrypt(Password, Key);
WriteString('Database', 'Password', tmpstr);
end;
finally
SYSINI.Free;
end;
end;
How to use ADO Connection's ConnectionString property in a flexible way using .udl files.
Product:Delphi 4.x (or higher)
Uploader: Marcus Neves
Question/Problem/Abstract:The ConnectionString property of the ADO Connection object is indeed very flexible, as long as you use IDataInitialize interface, which handles the Microsoft Data Link file (".udl").
Answer:
<> Abstract <> The ConnectionString property of the ADO Connection object is indeed very flexible, as long as you use IDataInitialize interface, which handles the Microsoft Data Link file (".udl") -- which is an ".ini" like file with just one section.
<> Microsoft Data Link Files <> The problem of creating and configuring a connection to a database using a UDL file is much more user-friendly than just using a simple .ini file, because Windows automatically recognizes a .udl file and opens up the "Data Link Properties" dialog box. To create a new .udl file is just a few seconds away: right-clicking on the Windows Explorer allows the user to select New and then Microsoft Data Link to create a .udl file. To edit it, just double-click the file to open up the properties dialog.
<> Creating a .udl file programmatically <> OLE-DB, which is the low-level layer used by ADO, has an interface named IDataInitialize that can be used to create a data source object using a connection string and also retrieve a connection string from an existing data source object. The interface has a method called WriteStringToStorage that we can use to write a default .udl file, if none is found. So, for instance, our MyApp application can have a default MyApp.udl file in the same directory and we use this file to connect to the database freeing the application from being recompiled to alter the database which its point to, allowing it to be database-independent (as long as you do not use any SQL specifc to a database). The sample procedure below will show how to use the method: ---------------------------------------------------------- // NOTE: the sample below uses unit files OLEDB and ComObj. procedure WriteUDLFile (const UDLFile: string);
const
// Default ConnectionString used by our application (SQL Server)
SConnect = 'Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=OurExampleDB;' +
'Data Source=OURSERVER;Packet Size=4096';
var
DataInitialize: IDataInitialize;
wUDLFile: array[0..MAX_PATH - 1] of WideChar;
begin
// Create the DataInitialize object
DataInitialize := CreateComObject(CLSID_DataLinks) as IDataInitialize; // Convert AnsiString parameter to WideChar
StringToWideChar (UDLFile, @wUDLFile, MAX_PATH); // Call method WriteStringToStorage with the default ConnectionString above.
if Failed(DataInitialize.WriteStringToStorage(wUDLFile, SConnect, CREATE_NEW)) then
raise Exception.Create('Can''t write UDL');
end; ----------------------------------------------------------
<> Delphi's VCL support <> I've just discovered that the Delphi VCL already comes with a procedure named CreateUDLFile that creates a basic .udl file (with just Provider and Data Source defined). The above code is very similar (was done with no knowledge of Delphi's own routine). If you take a look at the source code of ADODB.pas, you'll encounter the following public routines: procedure CreateUDLFile(const FileName, ProviderName, DataSourceName: WideString);
function DataLinkDir: string;
procedure GetProviderNames(Names: TStrings);
function PromptDataSource(ParentHandle: THandle; InitialString: WideString): WideString;
function PromptDataLinkFile(ParentHandle: THandle; InitialFile: WideString): WideString;
function GetDataLinkFiles(FileNames: TStrings; Directory: string = ''): Integer; I've found no documentation on the above procedures, but looking at the source code we can more precisely defines what it does. I'll comment briefly on each one here; but if you want more inside information, I'll reinforce you to look at the source code. It's the best way to learn! ** CreateUDLFile: This procedure creates a .udl file with just two properties: Provider and Data Source. ** DataLinkDir: Gets from the registry the default path to save .udl files, defined by Microsoft OLE-DB. ** GetProviderNames: Returns all the names of the Providers available on the system. ** PromptDataSource: Shows the Data Link Properties dialog box to enable the user to edit the ConnectionString easily. The ParentHandle parameter allows the dialog to be centered within a given window handle, which can be your main form (Handle property). ** PromptDataLinkFile: Opens the Select Data Link dialog box. Allows the user to browse and organize .udl files. Returns a fully qualified path to the user-selected .udl file. ** GetDataLinkFiles: Returns all found .udl files in the Directory given.
<> Using a .udl file <> To use a .udl file, just pass "File Name=MyUdlFile.udl" string to the DatabaseConnect property of the ADODatabase as show in the procedure below: ---------------------------------------------------------- procedure ConnectFromUDL (const UDLFile: String);
begin
ADODatabase.Close;
ADODatabase.DatabaseConnect := Format('File Name=%s', [UDLFile]);
ADODatabase.Open;
end; ----------------------------------------------------------
<> Conclusion <> OLE-DB and ADO are really a very good way to deal with different data sources and also allows the programmer to be very flexible dealing with them. This article here shows how to use .udl files to dynamically handles the connection to a database. Another alternative is to present the user with a dialog box where he/she can enter the server name, user name and password and dynamically create the ConnectionString property and use it to connect to the database. But using .udl files allow more flexibility as the user can deal with a Windows default dialog (the same as the ODBC dialog was in the past).
把ADOQUERY、ADODATASET等的ACTIVE改成FALSE
保存就行了
delphi6的sp2在哪下载?谢谢!
数据的问题, 当你现在用的服务器和原来的服务器名称不相同的时候这样就sql server就连接不上的。 如果你把现有的服务器名称改为你在家
里那时用的。 这样服务器也起不来,需要把你单位的电脑重新改名为你家里的电脑。重新装sql server. 如果你不想这么干的话,那就照楼上的兄弟说的做。把连接字符串写入.ini
文件,或者是注册表 (用户名,服务器/ip,密码),我常用的方法是注册表。在程序用的时候读出来连接起来送给adoconnection.adoconstring属性
别吓我哪:(我这是要交去应聘的,急得很,唉,不过我看我这水平,多半那个招聘单位也不敢要了,才学两天,赶鸭子上架,哎
实在不行的话,今天早上只有在单位不做了,回家再做,还是希望解决哪
等待中
其实原先DELPHI5下没有这个问题的
等我试试第二个问题大家的答案,再结贴
受教。
To: liuri(璇玑)
以上意见是正确的,先断开所有的连接。ADOconnection->connected、ADOdatabase-〉active .重新设置ADOconnection->connectstring即可。