使用C#写一段Oracle数据库连接代码
时间: 2024-01-11 11:02:44 浏览: 128
好的,下面是一个使用 C# 连接 Oracle 数据库的示例代码,需要使用到 `Oracle.ManagedDataAccess` 库。
```csharp
using System;
using Oracle.ManagedDataAccess.Client;
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source={数据库地址}:{端口号}/{服务名};User ID={用户名};Password={密码};";
try
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
Console.WriteLine("连接成功!");
// 在这里可以执行 SQL 查询、更新等操作
}
}
catch (Exception ex)
{
Console.WriteLine("连接失败:" + ex.Message);
}
Console.ReadKey();
}
}
```
其中 `{数据库地址}`、`{端口号}`、`{服务名}`、`{用户名}`、`{密码}` 需要替换为你实际使用的数据库连接信息。在 `using` 块中,我们创建了一个 `OracleConnection` 对象,并调用 `Open` 方法打开连接。接下来,你可以在该块中执行你需要的 SQL 操作。最后,记得关闭连接。
如果连接成功,程序会输出 `连接成功!`,否则会输出失败原因。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)