C#远程连接oracle数据库,以下是源代码,另外,本机未安装oracle客户端,运行后提示错误,求高手们解决
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace 连接远程oracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string connstring = "Data Source=(DESCRIPTION=" + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=1521)))" + "User ID=scott;Password=tiger;";
OracleConnection conn = new OracleConnection(connstring);
try
{
conn.Open();
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from emp";
OracleDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
textBox1.Text = "远程数据库时间:" + odr.GetOracleDateTime(0).ToString();
textBox1.Text = textBox1.Text + "远程数据库时间:" + odr.GetString(2);
}
odr.Close();
}
catch
{
MessageBox.Show("erro");
}
finally
{
conn.Close();
}
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace 连接远程oracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string connstring = "Data Source=(DESCRIPTION=" + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=IP)(PORT=1521)))" + "User ID=scott;Password=tiger;";
OracleConnection conn = new OracleConnection(connstring);
try
{
conn.Open();
OracleCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from emp";
OracleDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
textBox1.Text = "远程数据库时间:" + odr.GetOracleDateTime(0).ToString();
textBox1.Text = textBox1.Text + "远程数据库时间:" + odr.GetString(2);
}
odr.Close();
}
catch
{
MessageBox.Show("erro");
}
finally
{
conn.Close();
}
}
}
} 展开
1个回答
展开全部
报的什么错误?C#连接Oracle一般都要装客户端的,不装你的OracleClient是从哪里引入的呢?
连接字符串填充了参数么?还是就这样?
连接字符串填充了参数么?还是就这样?
追问
HOST=IP,其中的IP是远程数据的IP地址,我把客户端也安装上了,和服务器的版本是一样的,PLSQL Developer可以正常连接(主机字符串用“IP:1521/orcl”),在tnsnames.ora中也添加了如下文字:
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = IP)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
程序运行时调用了MessageBox.Show("erro");语句,显示"erro"提示。
追答
那应该是正常的。还是先试试把try语块里的语句范围缩小,逐步排除吧。Oracle在.NET下总是没有国民待遇,有时小病小痛的,只怪它不是微软的孩子。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询