一、找到Unity自带的Oracle数据库连接库(点击Unity程序图标-->右键打开文件位置“...\Editor\Data\Mono\lib\mono\2.0”路径下查找)
注意:...表示你自己的Unity安装路径:
二、将System.Data.OracleClient.dll库放入Unity的Asset文件下的Plugins文件夹下
三、编写测试脚本Test_OracleConnect.cs
①引入命名空间:using System.Data.OracleClient;
②使用:编写连接字符串:
string connString = string.Format(@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))
(CONNECT_DATA=(SERVICE_NAME = {2})));User Id={3};Password={4};",
"192.168.1.120", "1521", "ORCL", "test", "123456");
③连接数据库执行语句
/***
*Title:"三维可视化" 项目
*主题:测试Oracle数据库的连接
*Description:
*功能:XXX
*Date:2019
*Version:0.1版本
*Author:Coffee
*Modify Recoder:
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Data.OracleClient;
namespace TestFunction
{
public class Test_OracleConnect : MonoBehaviour
{
void Start()
{
string connString = string.Format(@"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))
(CONNECT_DATA=(SERVICE_NAME = {2})));User Id={3};Password={4};",
"192.168.1.120", "1521", "ORCL", "test", "123456");
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
string sql = @"select sysdate from dual";
//string sql = @"select * from ""PeopleInfo""";
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.ExecuteScalar();
Debug.Log(conn.State.ToString());
Debug.Log(cmd.ExecuteScalar().ToString());
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
conn.Close();
}
}
}//Class_end
}
四、执行效果:
五、注意Oracle在编写sql语句时:如果出现“Exception: ORA-00942: 表或视图不存在”问题;
原因:Oracle 是大小写敏感的,我们自己编写的Sql脚本语句Oracle会自动将我们的表名,字段名转成大写
解决办法:在表名或字段名加上""后,Oracle不会将其转换成大写;如下所示:
string sql = @"select * from ""PeopleInfo""";
注意:本内容参考:C# 不安装Oracle客户端情况下,如何连接到Oracle数据库
"ORA-00942: 表或视图不存在 "的原因和解决方法