在 C# 中连接达梦(DM, Dameng)数据库,并实现增删改查功能,通常使用 DmProvider 作为数据库连接驱动。以下是具体步骤和代码示例:
1. 安装达梦数据库驱动
- 可以从达梦官网获取 DM ADO.NET Provider,也可以安装
DmProvider
的 NuGet 包。
dotnet add package DmProvider
2. 引入命名空间
using Dm;
using System;
using System.Data;
3. 配置连接字符串
// 示例连接字符串
string connectionString = "Host=127.0.0.1;Port=5236;UserId=SYSDBA;Password=SYSDBA;Database=TESTDB";
4. 连接数据库
using (var connection = new DmConnection(connectionString))
{
connection.Open();
Console.WriteLine("连接成功!");
}
5. 实现增删改查功能
(1) 插入数据
public void InsertData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "INSERT INTO Employee (Id, Name, Age) VALUES (@Id, @Name, @Age)";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Id", 1));
command.Parameters.Add(new DmParameter("@Name", "John"));
command.Parameters.Add(new DmParameter("@Age", 30));
int result = command.ExecuteNonQuery();
Console.WriteLine($"插入 {result} 条记录");
}
}
}
(2) 查询数据
public void SelectData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM Employee";
using (var command = new DmCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}, Age: {reader["Age"]}");
}
}
}
}
(3) 更新数据
public void UpdateData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "UPDATE Employee SET Age = @Age WHERE Id = @Id";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Age", 35));
command.Parameters.Add(new DmParameter("@Id", 1));
int result = command.ExecuteNonQuery();
Console.WriteLine($"更新 {result} 条记录");
}
}
}
(4) 删除数据
public void DeleteData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "DELETE FROM Employee WHERE Id = @Id";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Id", 1));
int result = command.ExecuteNonQuery();
Console.WriteLine($"删除 {result} 条记录");
}
}
}
6. 完整示例
class Program
{
static string connectionString = "Host=127.0.0.1;Port=5236;UserId=SYSDBA;Password=SYSDBA;Database=TESTDB";
static void Main(string[] args)
{
InsertData();
SelectData();
UpdateData();
DeleteData();
SelectData();
}
public static void InsertData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "INSERT INTO Employee (Id, Name, Age) VALUES (@Id, @Name, @Age)";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Id", 1));
command.Parameters.Add(new DmParameter("@Name", "John"));
command.Parameters.Add(new DmParameter("@Age", 30));
command.ExecuteNonQuery();
Console.WriteLine("插入成功");
}
}
}
public static void SelectData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM Employee";
using (var command = new DmCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"Id: {reader["Id"]}, Name: {reader["Name"]}, Age: {reader["Age"]}");
}
}
}
}
public static void UpdateData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "UPDATE Employee SET Age = @Age WHERE Id = @Id";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Age", 35));
command.Parameters.Add(new DmParameter("@Id", 1));
command.ExecuteNonQuery();
Console.WriteLine("更新成功");
}
}
}
public static void DeleteData()
{
using (var connection = new DmConnection(connectionString))
{
connection.Open();
string sql = "DELETE FROM Employee WHERE Id = @Id";
using (var command = new DmCommand(sql, connection))
{
command.Parameters.Add(new DmParameter("@Id", 1));
command.ExecuteNonQuery();
Console.WriteLine("删除成功");
}
}
}
}
7. 注意事项
- 确保达梦数据库服务已经启动,并且连接字符串配置正确。
- 表结构可以为:
CREATE TABLE Employee (
Id INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
总结
本文介绍了 C# 连接达梦数据库的基本操作,包括增删改查。使用 DmProvider
提供的 DmConnection
、DmCommand
和 DmParameter
类,能够轻松实现对数据库的操作。