.NET 连接Oracle数据库 OLE DB .NET 数据提供程序和 Visual C# .NET 访问 Oracle 数据库

news/2024/7/3 23:06:18
この資料では、ADO.NET の OLE DB マネージ プロバイダを使用して Oracle データベースにアクセスする方法について、実例を基に説明します。

必要条件
必要なハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、および Service Pack は次のとおりです。 • Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server、Windows NT 4.0 Server のいずれか
• Oracle Client ツール (コンピュータにインストールされていること)
• Microsoft Visual Studio .NET
この資料は、次のトピックについて詳しい知識のあるユーザーを対象としています。 • Visual Studio .NET
• ADO.NET の基本および構文
• Oracle への接続

Oracle データベースへのアクセス手順
1. Oracle で次のステートメントを使用して、TestTable という名前のテーブルを作成します。

Create Table TestTable (c1 char(5));

2. 次のステートメントを実行して、TestTable にデータを挿入します。

Insert into TestTable c1 values('Test1');
Insert into TestTable c1 values('Test2');
Insert into TestTable c1 values('Test3');

3. Visual Studio .NET を起動します。
4. Visual C# .NET で新しい Windows アプリケーション プロジェクトを開きます。
5. プロジェクトに System.Data 名前空間への参照が含まれていることを確認し、含まれていない場合は追加します。
6. Button コントロールを Form1 に配置し、Name プロパティを btnTest に変更します。
7. 後続のコードで System、System.Data、System.Data.OleDb の名前空間を使用して宣言を修飾しなくてもよいようにするため、以下の例のように、using ステートメントに各名前空間を定義します。

using System;
using System.Data;
using System.Data.OleDb;

8. フォーム ビューに切り替え、[btnTest] をダブルクリックしてクリック イベント ハンドラを追加します。ハンドラに、以下のコードを追加します。
String sConnectionString =
"Provider=MSDAORA.1;User ID=myUID;password=myPWD;
Data Source=myOracleServer;Persist Security Info=False";
String mySelectQuery =
"SELECT * FROM TestTable where c1 LIKE ?";

OleDbConnection myConnection = new OleDbConnection(sConnectionString);
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);

myCommand.Parameters.Add("@p1", OleDbType.Char, 5).Value = "Test%";
myConnection.Open();
OleDbDataReader myReader = myCommand.ExecuteReader();
int RecordCount=0;
try
{
while (myReader.Read())
{
RecordCount = RecordCount + 1;
MessageBox.Show(myReader.GetString(0).ToString());
}
if (RecordCount == 0)
{
MessageBox.Show("No data returned");
}
else
{
MessageBox.Show("Number of records returned: " + RecordCount);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myReader.Close();
myConnection.Close();
}

9. プロジェクトを保存します。
10. [デバッグ] メニューの [開始] をクリックして、プロジェクトを実行します。
11. [btnTest] ボタンをクリックして、データを表示します。

========================================================================================

[img]http://dl.iteye.com/upload/picture/pic/116779/0915753d-f3cc-31dd-9619-7683664ca4bc.png[/img]

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.OleDb;
using System.Collections;
using System.Configuration;

namespace ConnectionOracle
{
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration("");
AppSettingsSection settings = (AppSettingsSection)config.GetSection("appSettings");
String connectionString = settings.Settings["connectionString"].Value;
String sqlString = settings.Settings["sqlString"].Value;

OleDbConnection conn = new OleDbConnection(connectionString);
OleDbDataReader reader;
DataTable table = new DataTable(); ;

try
{
conn.Open();

OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText =sqlString+ " and rownum<=10";

using (reader = cmd.ExecuteReader())
{

table.Load(reader);
}

reader.Close();
conn.Close();

Console.WriteLine(table.Columns);
dataGridView1.DataSource = table;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}

private void main_Load(object sender, EventArgs e)
{

}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (exitConfirm(e))
{
toRelease();
}
}
private bool exitConfirm(FormClosingEventArgs e)
{
bool exitFlag=false;
closeConfirm cc = new closeConfirm();
DialogResult dr = cc.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else
{
if (!cc.closePass.Text.Equals("123456"))
{
e.Cancel = true;
}
else
{
exitFlag = true;
}
}
return exitFlag;
}
private void toRelease() {

}
}
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="connectionString" value="Provider=MSDAORA;Data Source=ywinweb;Persist Security Info=True;Password=btw;User ID=btw"/>
<add key="sqlString" value="select ip_address,create_dt,model from t_log where log_level=0"/>
</appSettings>
</configuration>

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;

namespace ConnectionOracle
{
public partial class closeConfirm : Form
{
public closeConfirm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
}

http://www.niftyadmin.cn/n/3662573.html

相关文章

mysql主键自增方案哪种好

在只使用单数据库时&#xff0c;使用自增主键ID无疑是最适合的。 但在集群、主从架构上时就会有一些问题&#xff0c;比如&#xff1a;主键的全局唯一这里介绍一下在集群环境下除了自增ID外的其它创建主键的方案 1、通过应用程序生成一个GUID&#xff0c;然后和数据一起插入切分…

MySQL系列(一)

一、简介 MySQL是最流行的开放源码SQL数据库管理系统&#xff0c;它是由MySQL AB公司开发、发布并支持的。有以下特点&#xff1a; MySQL是一种数据库管理系统。 MySQL是一种关联数据库管理系统。 MySQL软件是一种开放源码软件。 MySQL数据库服务器具有快速、可靠和易于使用的特…

logback日志模板与详解

<pattern>的转换符说明&#xff1a; &#xff08;这部分引用自http://aub.iteye.com/blog/1103685&#xff09;转换符 作用 c {length } lo {length } logger {length }输出日志的logger名&#xff0c;可有一个整形参数&#xff0c;功能是缩短logger名&#xff0c;设置为…

php curl 错误 cURL error 60

为什么80%的码农都做不了架构师&#xff1f;>>> cURL error 60 unable to get local issuer certificate 原因 curl 配置中开启了证书校验 解决此报错有2种处理方法 1、curl 关闭证书校验 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 2、php 配置中添加证书…

Python基础之Python语言类型

编程语言主要从以下几个角度进行分类&#xff1a;编译型和解释型静态语言和动态语言强类型定义语言和弱类型定义语言编译和解释的区别是什么&#xff1f;编译器把源程序的每一条语句都编译成机器语言&#xff0c;并保存成二进制文件&#xff0c;这样运行时计算机可以直接以机器…

跨程序共享数据——Content Provider 之 创建自己的内容提供器 ...

本模块共有四篇文章&#xff0c;参考郭神的《第一行代码》&#xff0c;对Content Provider的学习做一个详细的笔记&#xff0c;大家可以一起交流一下&#xff1a; 跨程序共享数据——Content Provider 之 运行时权限解析以及申请的实现&#xff08;可完美解决java.lang.Securit…

Python常用模块之时间(datetime,time)

首先说一下datetime 时间戳转换为指定格式日期&#xff1a; 用strftime格式化时间的参数 python中时间日期格式化符号&#xff1a; %y 两位数的年份表示&#xff08;00-99&#xff09; %Y 四位数的年份表示&#xff08;000-9999&#xff09; %m 月份&#xff08;01-12&#xff…

有关ultraedit语法高亮的设置

刚安装ultraedit后编写文档可能会遇到语法关键字不高亮的问题&#xff0c;以下为解决方案。 1.找到高级——配置——设置 。 2.在设置中依次找到编辑器显示——语法高亮 。 3.在文档的完整目录名称中填入你ultraedit安装目录下wordfiles文件夹的完整路径 。 &#xff08;此处一…