c#连接远程oracle数据库

主要有两种方法,一种是直接写连接字符串,令一种是将连接字符串下载web.config文件中,下面分别作说明:
直接将写连接字符串:

 private static string connstr = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Mhost) 

(PORT=mport)))(CONNECT_DATA=(SERVICE_NAME=mywervicename)));Persist Security Info=True;User Id=myusername; 

Password=mypassword";
 private OracleConnection DBCONN = new OracleConnection(connstr);

将连接字符串写在web.config文件中

private static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
private OracleConnection DBCONN = new OracleConnection(connstr);

这种方式的话要在配置文件中加入下面的配置信息

 <connectionStrings>
    <add name="ConnectionString" connectionString="Password=password;User ID=username;Data Source=datasource;Persist Security Info=True"/>
  </connectionStrings>

然后就可以打开连接并查询数据了
如下面

  public void GetData()
        {


            if (DBCONN.State == ConnectionState.Closed)
            {
                DBCONN.Open();
                string quarystr = "select * from tablename";
                OracleCommand comm = new OracleCommand(quarystr, DBCONN);
                OracleDataReader reader;
                reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    string str = "" + reader.GetString(1);
                }
            }

        }

发表评论

电子邮件地址不会被公开。 必填项已用*标注