How to maintain COM + status information in Delphi

Source: Internet
Author: User
Tags db2 connect

The problem starts with this: I need to write a COM + to connect to different databases. Some friends might say that you should build a COM + for each database, but not in my system. We are doing an education-assisted system, the user is a school (including school teachers, students, parents), we build a database for each school, these databases are the same structure.

Of course, we also have a management database, used to coordinate the relationship between the databases. Each additional school user, we activate a new database for our customers, that is, our database is growing in number, and we have only one client, we don't develop different clients for each school, and our COM + has only one set, not one for each database. So I have to get it to connect to a different database in COM + based on the user's identity.

Obviously, this COM + should provide a way to let the caller (either the client application or the other middleware) choose the connected database, in practice we find its database name in the management library based on the user ID, and then connect the user database, where, in order to simplify the problem, We think that the caller already knows the name of the database and calls the database directly.

Add a private member dbname:string to the COM + class to hold the name of the database to which you want to connect. You should also provide a way to set its value, which I started to write.

procedure TmtsDBConn.ConnectTo(sDBName:string);
begin
try
DBName:=sDBName;
SetComplete;
Except
SetAbort;
end;
end;

Then put the Adoconnection,adodataset, and the Datasetprovider control, respectively, named ADOC,ADODS,DSP. Set the connection between them, set the ADOc connection string to the connection database "DB1", which is the default value, and then in the ADOc Beforeconnect event:

ADOc. connectionstring:=connectstringa+ ' Initial catalog= ' +dbname+ '; +CONNECTSTRINGC;

The Connectstringa and CONNECTSTRINGC here are for dynamically building connection strings, preset string constants, as follows:

const
ConnectStringA='Provider=SQLOLEDB.1;Password=2003;Persist Security Info=True;User ID=sa;';
ConnectStringB='Initial Catalog=DB1;';
ConnectStringC='Data Source=server3;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=LXM;Use Encryption for Data=False;Tag with column collation when possible=False';

Compile and install this COM +. Then write the client program to call it.

Place a dcomconnection in the client program, connect to the write COM + server above, place a clientdataset, set its remoteserver and provider properties, and write the SQL statement in its CommandText. Then, put the datasource control and the DBGrid control to establish the connection between them. Finally, put a button in its Click event:

Dcomconnection1.Connected:=true;
Dcomconnection1.AppServer.connect('DB2');
ClientDataset1.Active:=true;
Dcomconnection1.Connected:=false;

This code is meant to test if you can access the data in the DB2 database. But the result is, click the button, always error, what is the reason?

Go back to COM + 's project, debug it, set breakpoints in ConnectTo and Adocbeforeconnect, and discover that the program executes to

Dbname:=sdbname, it is true that the dbname value has been set to "DB2", but in the execution

ADOc. connectionstring:=connectstringa+ ' Initial catalog= ' +dbname+ '; +CONNECTSTRINGC, the dbname again becomes an empty string, so it goes wrong.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.