如何使用 C# 将不同方法的结果加载到变量中

How to load Results from a different method into a variable using C#

我的主要方法正在调用另一个方法 (SqlConnector),该方法正在从 SQL 数据库中获取结果。我需要将 SqlConnector 方法的结果加载到一个名为 "ID".

的变量中

执行此操作时没有任何反应。我认为我的 SqlConnector 方法没有被调用。

private static void Main(string[] args)
{
    string SyncType = args[0];
    try
    {
        if (SyncType == "Compute")
        {
            string InitialSQLStatement = ($"exec StartUsage '{SyncType}'");
            var ID = SqlConnector(InitialSQLStatement);

            int RowID = Convert.ToInt32(ID);
            Console.WriteLine($"{ID},{RowID}");
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));

            ...Calling a different Class in case of Compute
            Compute Compute = new Compute();
            var task = Compute.GetBaseDetails(RowID);
            task.Wait();
        }

        else if (SyncType == "Blob")
        {
            ...Calling a different Class in case of Blob
        }

        else if (SyncType == "FileShare")
        {
            ...Calling a different Class in case of FileShare
        }

    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.Message}");
    }
}

public static SqlDataReader SqlConnector(string SQLStatement)
{
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = Properties.Settings.Default.SQLServerName;
    builder.UserID = Properties.Settings.Default.SQLServerAdmin;
    builder.Password = Properties.Settings.Default.SQLServerAdminPasword;
    builder.InitialCatalog = Properties.Settings.Default.DatabaseName;

    using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
    {
        StringBuilder sb = new StringBuilder();

        using (SqlCommand command = new SqlCommand(SQLStatement, connection))
        {
            connection.Open();
            SqlDataReader Executed = command.ExecuteReader();

            connection.Close();
            return Executed;
        }
    }
}

我认为您的问题是,您无法将 SqlReader 转换为整数。您必须先阅读,然后将字符串转换为整数。我没有测试代码,但这应该可以。

private static void Main(string[] args)
    {
        string SyncType = args[0];
        try
        {
            if (SyncType == "Compute")
            {
                string InitialSQLStatement = ($"exec StartUsage '{SyncType}'");
                int RowID = ConnectAndReadID(InitialSQLStatement);
                Console.WriteLine($"{RowID}");
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));

                //...Calling a different Class in case of Compute
                Compute Compute = new Compute();
                var task = Compute.GetBaseDetails(RowID);
                task.Wait();
            }

            else if (SyncType == "Blob")
            {
                //...Calling a different Class in case of Blob
            }

            else if (SyncType == "FileShare")
            {
                //...Calling a different Class in case of FileShare
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }
    }

    public static int ConnectAndReadID(string SQLStatement)
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = Properties.Settings.Default.SQLServerName;
        builder.UserID = Properties.Settings.Default.SQLServerAdmin;
        builder.Password = Properties.Settings.Default.SQLServerAdminPasword;
        builder.InitialCatalog = Properties.Settings.Default.DatabaseName;

        using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
        {
            StringBuilder sb = new StringBuilder();

            using (SqlCommand command = new SqlCommand(SQLStatement, connection))
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    string str = reader.GetString(0); // Read first column
                    connection.Close();
                    return Convert.ToInt32(str);
                }
                else
                {
                    // Your query fails
                    connection.Close();
                    return -1;
                }
            }
        }
    }

    public static List<int> ConnectAndReadIDs(string SQLStatement)
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = Properties.Settings.Default.SQLServerName;
        builder.UserID = Properties.Settings.Default.SQLServerAdmin;
        builder.Password = Properties.Settings.Default.SQLServerAdminPasword;
        builder.InitialCatalog = Properties.Settings.Default.DatabaseName;

        using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
        {
            StringBuilder sb = new StringBuilder();

            using (SqlCommand command = new SqlCommand(SQLStatement, connection))
            {
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                List<int> Ids = new List<int>(); // Create a List since you have multiple ids
                while (reader.Read()) // Instead of checking once if the reader has data, read rows until it doesnt have data anymore
                {
                    string str = reader.GetString(0); // Read first column
                    Ids.Add(Convert.ToInt32(str)); // Add the value to the Ids List
                }
                connection.Close();
                return Ids; // Return all Ids
            }
        }
    }