简体   繁体   中英

Connect to Oracle 11g in C# Application Using ODBC and User Input Connection String

I'm working on a C# application which we would like to connect to an Oracle database using ODBC. When I attempt to open the connection, it gives me "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". It is trying to connect to an Oracle 11g database with the following methods:

    public OdbcConnectionStringBuilder buildOdbcConnectionString(string InitialCatalog)
    {
        OdbcConnectionStringBuilder connStr = new OdbcConnectionStringBuilder();

        connStr.Driver = "{Microsoft ODBC for Oracle};";

        connStr.ConnectionString = "Driver={Microsoft ODBC for Oracle};" + "CONNECTSTRING=(DESCRIPTION="
                + "(ADDRESS=(PROTOCOL=TCP)(HOST=" + tbDbServer.Text + ")(PORT=1521))"
                + "(CONNECT_DATA=(SERVICE_NAME=" + InitialCatalog + ")));"
                + "User Id=" + tbUser.Text + ";Password=" + tbPassword.Text + ";"
                + "Connection Timeout = 300;";

        return connStr;
    }

    public DbConnection(string name, OdbcConnectionStringBuilder odbcStringBuilder )
    {
        try
        {
            Name = name;
            odbcConnection = new OdbcConnection(odbcStringBuilder.ConnectionString);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    public void Open(OdbcConnection testConn)
    {
        if (testConn.State == ConnectionState.Closed)
        {
            testConn.Open();
        }
    }

    public Boolean testConnection(OdbcConnection testConn)
    {
        try
        {
            Open(testConn);
            Close(testConn);
            return true;
        }
        catch
        {
            return false;
        }
    }

Any ideas on what I need to adjust to allow it to connect?

I don't think you can put all that Oracle stuff in the connection string but in any case it looks like the ODBC driver manager is either confused by your connect string or you don't have the Microsoft ODBC for Oracle driver installed. If you have that driver installed then an easy way to work out the connection string is to a) create a DSN using the driver manager dialogues, b) test it works c) connect in your code using DSN=mydsn d) examine the out connection string SQLDriverConnect can return (that is the C API but I presume in C# you can get the same info). Then you can delete your DSN and use the string SQLDriverConnect returned for a DSN-less connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM