时间:2021-07-01 10:21:17 帮助过:70人阅读
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;using System.Data.SqlClient;name
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; using System.Data.SqlClient; namespace TEST { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //string connstring = "Data Source=ouc131;user=ouc;password=letu;";//如果本地有oracle客户端的话,用这种连接 string connstring = "User Id=ouc;Password=letu;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=222.195.*.*)(PORT=1523))(CONNECT_DATA=(SID=ouc)));Unicode=True;"; OracleConnection conn = new OracleConnection(connstring); try { conn.Open(); OracleDataAdapter AdapterSelect = new OracleDataAdapter("select * from text", conn); DataTable dt = new DataTable(); AdapterSelect.Fill(dt); dataGridView1.DataSource = dt.DefaultView; } catch (Exception ee) { MessageBox.Show(ee.Message); } finally { conn.Close(); } } } }
1.到oracle官网下载oracle client instant(http://www.oracle.com/technetwork/topics/winsoft-085727.html)解压后的dll来复制到你的exe文件夹中.( instantclient-basic-nt-11.2.0.2.0.zip 支持多国语言, instantclient-basiclite-nt-11.2.0.2.0.zip 只支持英文, 高版本的oracle client可连结低版本的oracle)
(或者如果你本机安装个oracle客户端, 可以在oracle客户端文件夹搜索下*oci*.dll(oci是oracle call interface)文件, 搜索的全部dll复制到你的exe文件夹中, 网上常的复制dll就是这几个dll吧)
2.连结字符串要写成(可以写到配置文件中的, 就是要写直实的地址, 端口,实例,用户名,密码)
private static string CONNECTION_STRING = "User Id=myUserID;Password=myPassword;Data Source=(DESCRIPTION=" + "(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.server.com)(PORT=yourPort#))" + "(CONNECT_DATA=(SID=yourSID)));";
这样就不用去读取安装客户端的tnsnames.ora中配置了
注意: 在编程, 要把刚上面的dll放到你的debug或release目录下, 就是与exe文件同目录, 不然找不到文件, 各种出错.