oracle数据库代码怎么执行,C#连接Oracle数据库(执行增删改查操作)
时间: 2024-01-26 10:02:42 浏览: 159
要执行Oracle数据库代码,首先需要安装Oracle数据库客户端并设置环境变量,然后使用SQL*Plus或SQL Developer等工具连接数据库并执行代码。以下是C#连接Oracle数据库并执行增删改查操作的示例代码:
```csharp
using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
namespace OracleDemo
{
class Program
{
static void Main(string[] args)
{
string connectionString = "Data Source=<database>;User Id=<username>;Password=<password>";
string query = "SELECT * FROM <table>";
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
// 查询数据
using (OracleDataAdapter adapter = new OracleDataAdapter(query, connection))
{
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["<column>"]);
}
}
// 插入数据
using (OracleCommand command = new OracleCommand("INSERT INTO <table> (<column1>, <column2>) VALUES (:value1, :value2)", connection))
{
command.Parameters.Add(":value1", OracleDbType.Varchar2).Value = "<value1>";
command.Parameters.Add(":value2", OracleDbType.Int32).Value = 123;
int rows = command.ExecuteNonQuery();
Console.WriteLine(rows + " rows inserted");
}
// 更新数据
using (OracleCommand command = new OracleCommand("UPDATE <table> SET <column> = :value WHERE <condition>", connection))
{
command.Parameters.Add(":value", OracleDbType.Varchar2).Value = "<new value>";
int rows = command.ExecuteNonQuery();
Console.WriteLine(rows + " rows updated");
}
// 删除数据
using (OracleCommand command = new OracleCommand("DELETE FROM <table> WHERE <condition>", connection))
{
int rows = command.ExecuteNonQuery();
Console.WriteLine(rows + " rows deleted");
}
connection.Close();
}
Console.ReadLine();
}
}
}
```
其中,`<database>`、`<username>`、`<password>`、`<table>`、`<column>`、`<value>`和`<condition>`需要替换为实际的值。可以根据需要调整数据类型和参数名称。
阅读全文