简体   繁体   English

C#连接到Oracle数据库

[英]C# connection to Oracle database

I have been doing applications on Java, connected with MySQL, but now I am doing C# with Oracle. 我一直在使用Java和与MySQL连接的应用程序进行开发,但是现在我正在使用Oracle进行C#。 Here is the code I've got so far: 这是到目前为止我得到的代码:

using System.Data.OracleClient;

namespace Chat
{
    class DBconnector
    {
        static private string GetConnectionString()
        {
            return "Data Source=myserver.server.com;Persist Security Info=True;" +
                "User ID=myUserID;Password=myPassword;Unicode=True";
        }
        static public void ConnectAndQuery()
        {

            string connectionString = GetConnectionString();
            using(OracleConnection conn = new OracleConnection())
            {
                conn.ConnectionString = connectionString;
                conn.Open();
                Console.WriteLine("State: " + conn.State);
                Console.WriteLine("Connction String: " + conn.ConnectionString);

                OracleCommand command = conn.CreateCommand();
                string sql = "SELECT * FROM users";
                command.CommandText = sql;

                OracleDataReader reader = command.ExecuteReader();
                while(reader.Read())
                {
                    string myField = (string)reader["MYFIELD"];
                    Console.WriteLine(myField);
                }
            }

        }

    }
}

What is hacking me is that I don't know what to type in exchange of "myserver.server.com", "myUserID" and the "myPassword" in the connectionString. 搞砸我的是,我不知道在connectionString中交换“ myserver.server.com”,“ myUserID”和“ myPassword”时要键入什么。 I suppose it's "localhost/" and smth like that, but with Oracle I don't really have the same visual interface as with MySQL in the browser and thus I am kinda' lost. 我想它是“ localhost /”,类似这样,但是对于Oracle,我的浏览器与MySQL的视觉界面实际上并不相同,因此我有点迷失了。

I followed this tutorial: Instant Oracle using C# and I am doing the case with including the connection String directly in my code, but not using the tsanames.ora external file. 我遵循了本教程: 使用C#的Instant Oracle,并且正在通过在我的代码中直接包含连接String而不是使用tsanames.ora外部文件来解决这种情况。 Long story short -> I am not sure how to modify the connection string for my own database and if there are any other mistakes or suggestions - feel free to state them. 长话短说->我不确定如何为自己的数据库修改连接字符串,如果还有其他错误或建议,请随时声明。

I'm not sure if you can do this without modifying your tnsnames, but it's not hard: 我不确定是否可以在不修改tnsnames的情况下执行此操作,但这并不困难:

YOURSERVER = (DESCRIPTION = (ADDRESS = (PROTOCOL= TCP)
(Host= <your_server_hostname_or_IP>)(Port= <port>))(CONNECT_DATA = (SID = <DB_instance name>)) )

If you have doubts on how to fill these up, you should check with your nearest DBA. 如果您对如何填写这些表存有疑问,则应咨询离您最近的DBA。

Then just add YOURSERVER in: 然后只需在以下位置添加YOURSERVER:

return "Data Source=YOURSERVER; ...

Username and password are those related to the schema you want to connect. 用户名和密码是与您要连接的架构相关的用户名和密码。

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

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