简体   繁体   English

使用ODBC和用户输入连接字符串连接到C#应用程序中的Oracle 11g

[英]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. 我正在开发一个C#应用程序,我们希望使用ODBC连接到Oracle数据库。 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". 当我尝试打开连接时,它给我“错误[IM002] [Microsoft] [ODBC驱动程序管理器]数据源名称未找到且未指定默认驱动程序”。 It is trying to connect to an Oracle 11g database with the following methods: 它正尝试通过以下方法连接到Oracle 11g数据库:

    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. 我认为您无法将所有Oracle内容都放在连接字符串中,但是无论如何,看起来ODBC驱动程序管理器要么被您的连接字符串所迷惑,要么您没有安装Microsoft ODBC for Oracle驱动程序。 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). 如果已安装该驱动程序,则计算连接字符串的一种简单方法是:a)使用驱动程序管理器对话框创建一个DSN,b)测试其是否正常工作c)使用DSN = mydsn连接到您的代码中d)检查out连接字符串SQLDriverConnect可以返回(这是C API,但我认为在C#中您可以获得相同的信息)。 Then you can delete your DSN and use the string SQLDriverConnect returned for a DSN-less connection. 然后,您可以删除您的DSN,并为无DSN的连接使用返回的字符串SQLDriverConnect。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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