c#v 数据库存储过程
时间: 2023-06-29 07:20:38 浏览: 51
C# 中可以通过 ADO.NET 库的 SqlCommand 类来执行数据库存储过程。下面是一个示例:
```csharp
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("存储过程名称", connection))
{
command.CommandType = CommandType.StoredProcedure;
// 添加存储过程参数
command.Parameters.AddWithValue("@参数名", 参数值);
// 执行存储过程
SqlDataReader reader = command.ExecuteReader();
// 处理查询结果
while (reader.Read())
{
// 读取结果集中的每一行数据
}
}
}
```
其中,`connectionString` 是连接字符串,`存储过程名称` 是要执行的存储过程的名称,`@参数名` 是存储过程的参数名,`参数值` 是存储过程的参数值。在执行存储过程之前,需要使用 `command.Parameters.AddWithValue()` 方法添加存储过程的参数。执行存储过程后,可以通过 SqlDataReader 对象读取查询结果集中的每一行数据。