首页 > 解决方案 > 如何使用 c# 在 SQL Server 中循环遍历不同的 ID

问题描述

我尝试在 stackoverflow 上搜索类似的答案,但我可能只是不擅长搜索。我对 c# 比较陌生,我想做的是遍历不同的 ID,这样我就可以在 SQL Server 中使用准备好的语句来运行一系列查询,循环遍历这些不同的 ID。我厌倦了使用 while 循环,但我可能做错了,我不确定。我知道如何使用 SQL 服务器,这不是问题,因为我在那里工作得很好,但我想尝试在 C# 中将它用于不同的测试目的。这是我的代码:

using System;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
        Console.WriteLine("Executing query...");
        int i = 46556;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        while (true)
        {
            using (SqlConnection connection = new SqlConnection("CONNECTION_STRING"))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand(
                    "SELECT TOP 100 GlobalCustomerVehicleID, GlobalCustomerID, Status, VIN, LastEditByUserID, CreatedByUserID, Year, Make, Model FROM Drop_GlobalCustomerVehicle WHERE GlobalCustomerID = @ID", connection))
                {

                    command.Parameters.Add(new SqlParameter("ID", i));
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        int GlobalCustomerVehicleID = reader.GetInt32(0);
                        int GlobalCustomerID = reader.GetInt32(1);
                        string Status = reader.GetString(2);
                        string VIN = reader.GetString(3);
                        int LastEditByUserID = reader.GetInt32(4);
                        int CreatedByUserID = reader.GetInt32(5);
                        short Year = reader.GetInt16(6);
                        string Make = reader.GetString(7);
                        string Model = reader.GetString(8);
                        Console.WriteLine("GlobalCustomerVehicleID = {0}, GlobalCustomerID = {1}, Status = {2}, VIN = {3}, LastEditByUserID = {4}, CreatedByUserID = {5}, Year = {6}, Make = {7}, Model = {8}",
                            GlobalCustomerVehicleID,
                            GlobalCustomerID,
                            Status,
                            VIN,
                            LastEditByUserID,
                            CreatedByUserID,
                            Year,
                            Make,
                            Model
                            );
                    }

                }
            }
            i = i + 10;
            if (i > 47000)
                break;
        }

       stopwatch.Stop();
       Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);


        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

}

我知道其中一些并不是超级安全,例如直接在代码内部的连接字符串,但这仅用于测试目的,不会在内部测试之外使用。有人可以告诉我哪里出错了,甚至可以建议修复吗?感谢您的时间。

编辑:这是一个简单的修复,我很抱歉我的问题太笼统了!问题是我希望它实际上循环多次而不是一次。它的工作原理是它只进行了一次查询,但没有通过我期望的不同 ID。但是,我错过了一个 0,即使在搜索了我的代码之后,我也完全错过了那个非常容易的错误。感谢您指出这一点,并建议一种更优化的方式来编写 while 循环。

标签: c#sql-serverloops

解决方案


如果要使用多个 Id 进行搜索,则应使用INsql 中的子句。

SELECT TOP 100 GlobalCustomerVehicleID, GlobalCustomerID, Status, VIN, LastEditByUserID, CreatedByUserID, Year, Make, Model FROM Drop_GlobalCustomerVehicle WHERE GlobalCustomerID IN (ID1,ID2,ID3)

推荐阅读