C # the ODBC configuration dialog box is displayed in the program.

Source: Internet
Author: User
Tags odbc connection
Bool T;
Msdasc. datalinksclass A = new msdasc. datalinksclass ();
ADODB. connectionclass c = new ADODB. connectionclass ();
System. object B = (object) C;
T = A. promptedit (ref B );

 

This step-by-step article demonstrates how to use the data link Properties dialog box to programmatically create a connection string at design time.

Requirementsthe following list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 servermicrosoft Visual Studio. netthis article assumes that you are familiar with the following topics: Visual Studio. netado. net fundamentals and syntaxactivex Data Objects (ADO) fundamentals and syntaxcreate An ADO connectionfor Component Object Model (COM) Interoperability, you must declare the ADO connection object as ADODB. _ connection and cast the generic object that the data link returns to an ADODB. _ connection. start Visual Studio. net, and then create a new Visual C # windows application project. form1 is added to the project by default. from the project menu, click Add reference. on the com tab, select the following references: Microsoft ActiveX Data Objects 2.7 Microsoft oledb service component 1.0 type libraryif you are prompted to have a wrapper generated for you, click yes. add a button control to form1.add the following code to the button#click event: msdasc. datalinks mydlg = new msdasc. datalinks (); ADODB. _ connection adocon; // cast the generic object that promptnew returns to an ADODB. _ connection. adocon = (ADODB. _ connection) mydlg. promptnew (); adocon. open ("", 0); If (adocon. state = 1) {MessageBox. show ("connection opened"); adocon. close ();} else {MessageBox. show ("connection failed");} press the F5 key to compile and run the project, and then click button1.type the appropriate information in the Data Link Properties dialog box, and make sure that you select the allow saving password check box. click test connection. click OK. if the connection test succeeded in the Data Link, a connection to the database is established, and a message box is displayed. create an ole db connectionwhen you create an ole db connection with the ole db managed provider in. net, you cannot use the data link to create connections to ODBC data sources. because ODBC has its own managed provider in. net, you receive an error if you use the Microsoft oledb provider for ODBC drivers option in the Data Link Properties dialog box. in addition, you must load ADO into the application because the data link creates an object, which you cast to an ADODB. _ connection, that is not compatible with the oledbconnection object. thus, you must create an ADODB. _ connection and assign its connectionstring property to the connectionstring property of the oledbconnection object for this to work properly. start Visual Studio. net, and then create a Visual C # windows application project. form1 is added to the project by default. from the project menu, click Add reference. on the com tab, select the following references: Microsoft ActiveX Data Objects 2.7 Microsoft oledb service component 1.0 type libraryadd a button control to form1.add the following code to the top of the code window: using system. data. oledb; Add the following code in the button#click event: msdasc. datalinks mydlg = new msdasc. datalinks (); oledbconnection olecon = new oledbconnection (); ADODB. _ connection adocon; // cast the generic object that promptnew returns to an ADODB. _ connection. adocon = (ADODB. _ connection) mydlg. promptnew (); olecon. connectionstring = adocon. connectionstring; olecon. open (); If (olecon. state. tostring () = "open "){ MessageBox. Show ("connection opened "); Olecon. Close ();} else { MessageBox. show ("connection failed");} press F5 to compile and run the project, and then click button1.type the appropriate information in the Data Link Properties dialog box, and make sure that you select the allow saving password check box. click test connection. click OK. if the connection test succeeded in the Data Link, a connection to the database is established, and a message box is displayed. additional informationit requires additional effort to use this method to create an ODBC connection because the data link creates a connection string that is specific to ole db and is not compatible with the ODBC managed provider. for this to work, you must parse the ADODB connection string for the relevant information such as the user ID, password, and data source. after you obtain this information, you can use it to create a connection string that is specific to ODBC. keep in mind that the data link only uses ODBC Data Source names (DSNS); thus, you cannot create a DSN-less connection through the data link.
Related Article

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.