Microsoft is used to implement this function. data. connectionui. DLL and Microsoft. data. connectionui. dialog. DLL, two dynamic libraries, can be found under the common7 \ ide directory of vs's installation directory.
Add references. For simplicity, it is very convenient for vs to support several database types with the support of these two dynamic databases.
CodeAs follows:
Microsoft. Data. connectionui. dataconnectiondialog diag =NewMicrosoft. Data. connectionui. dataconnectiondialog (); Microsoft. Data. connectionui. datasource. addstandarddatasources (DIAG); Microsoft. Data. connectionui. dataconnectiondialog. Show (DIAG );
The effect after running is as follows:
It should be displayed in English, but you only need to create an ZH-CHS directory under the bin directory. data. connectionui. dialog. resources. after the DLL is run, it is displayed on the Chinese interface.
If you want to define a data source to support all dataprovider, you must first create a new datasource and then add dataprovider to the datasource.
The Code is as follows:
Microsoft. data. connectionui. dataconnectiondialog diag = New Microsoft. data. connectionui. dataconnectiondialog ();
Datasource d =NewDatasource ("Custom Type 1","All dataprovider supported");
D. providers. Add (dataprovider. odbcdataprovider );
D. providers. Add (dataprovider. oledbdataprovider );
D. providers. Add (dataprovider. oracledataprovider );
D. providers. Add (dataprovider. sqldataprovider );
Datasource D2 =NewDatasource ("Custom Type 2","Support some dataprovider");
D2.providers. Add (dataprovider. odbcdataprovider );
D2.providers. Add (dataprovider. oledbdataprovider );
Diag. CES. Add (d );
Diag. CES. Add (D2 );
Dataconnectiondialog. Show (DIAG );
After the above Code is run, the effect is shown on the right. On the left, only one datasource is added and all dataprovider is added. The Effect on the right is similar to that created.
Now I have a problem. Now all I have to add is the dataprovider that comes with vs. What should I do if I want to create a connectionui to connect to SQLite or support connectionui of MySQL? Although dataprovider can be added by itself, after being added to datasource, the running effect is not what I want.
Use the following code:
Dataconnectiondialog diag = New Dataconnectiondialog (); datasource d = New Datasource ( "SQLite" , "SQLite data source" ); Dataprovider P = New Dataprovider ( "SQLite data provisionProgram" , "SQLite data provider" , "SQLite" , "Used to connect to the SQLite Database" ); D. providers. Add (p); diag. CES. Add (d); dataconnectiondialog. Show (DIAG );
the running effect is as follows:
there is only one connectionstring attribute. No matter what I write in it, the connection test is always successful.
SQLite has an ADO. NET 2.0/3.5 SQLite data provider. After I install the SQLite data source, it runs as follows:
In fact, this effect is what I want. I don't know how vs displays this dialog box. How to implement it if you want to use it in your own program. I only know the installed ADO. NET 2.0/3.5 SQLite data provider, there should be a dynamic library to support this dialog box, SQLite. designer. DLL, which encapsulates a sqliteconnectionuicontrol. I think the effect shown in the figure above is the sqliteconnectionuicontrol. Just tested, this sqliteconnectionuicontrol should be shown in Chinese and English in the figure above, from the database to the password below. it is still a little troublesome to reuse it directly. I don't know whether there is a good way for high people.
here we use SQLite as an example. To achieve the same functions of MySQL, we have the same problem because they are not part of the dataprovider provided by.
which one has a good solution? I hope to show it.
my tags: vs tool Resource Mining, database connection selection