using System;
using System.Data.SqlClient;
class Program
{
    static void Main()
    {
        string connectionString = "your_connection_string_here";
        // 创建连接对象
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            // 打开数据库连接
            connection.Open();
            // 定义SQL查询语句
            string sqlQuery = "SELECT * FROM Employees";
            // 创建SqlCommand对象
            using (SqlCommand command = new SqlCommand(sqlQuery, connection))
            {
                // 创建SqlDataReader对象
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    // 使用游标遍历结果集
                    while (reader.Read())
                    {
                        // 获取每一行的数据
                        Console.WriteLine($"ID: {reader["ID"]}, Name: {reader["Name"]}");
                    }
                }
            }
        }
    }
}这段代码展示了如何使用 SqlDataReader 来模拟游标的行为,逐行读取和处理查询结果。
上一篇:c# do while
下一篇:c# object
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站