将两个参数从单独的 using 语句传递到函数中

Passing two arguments from separate using statements into a function

为了从 Sql 服务器数据库读取数据,有人告诉我必须将 reader 放在 using 语句中。还有一个问题Read data from SqlDataReader与此类似,但对我帮助不大,因为我已经知道如何读取数据了。我只是不知道如何将数据放入函数中。问题是我需要使用两个不同 using 语句中的每条数据,我需要将它们放入同一个函数 SendPushNotification() 中。但是,每次使用一个 reader 时,我都会丢失数据。一条 using 语句将数据添加到

string readerTest;

并且一个 using 语句将数据添加到

string uriReadString;

然后我继续将两者作为参数传递到我的函数中:

SendPushNotification(uriReadString, readerTest);

当它们都能够被放入函数时,readers 已经结束,我的字符串变回空的。请帮助。

using (var connection = new SqlConnection(connectionString))
{
    connection.Open();

    DateTime now = DateTime.Now;
    now = now.AddMilliseconds(-now.Millisecond);

    var command = new SqlCommand("SELECT ImageName FROM Images WHERE NotifyDate = @todayDate", connection);
    var paramDate = new SqlParameter("@todayDate", now);
    command.Parameters.Add(paramDate);

    var commandUri = new SqlCommand("SELECT * FROM Uri", connection);
    string readerTest = "";
    string uriReadString = "";
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {

            readerTest = reader[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetIDNow.txt", readerTest);

        }
    }

    using (SqlDataReader readerUri = commandUri.ExecuteReader())
    {
        while (readerUri.Read())
        {
            uriReadString = readerUri[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetUriNow.txt", uriReadString);

        }

    }
    SendPushNotification(uriReadString, readerTest);

}

我相信这就是您想要做的...如果不是,我将删除答案。将字符串手动添加到列表中,然后以并行方式,恭敬地推送通知。

List<string> readList = new List<string>();
List<string> uriList = new List<string>();
using (var connection = new SqlConnection(connectionString))
{
    connection.Open();

    DateTime now = DateTime.Now;
    now = now.AddMilliseconds(-now.Millisecond);

    var command = new SqlCommand("SELECT ImageName FROM Images WHERE NotifyDate = @todayDate", connection);
    var paramDate = new SqlParameter("@todayDate", now);
    command.Parameters.Add(paramDate);

    string readerTest = "";
    string uriReadString = "";
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {

            readerTest = reader[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetIDNow.txt", readerTest);
            readList.Add(readerTest);
        }
    }

    using (SqlDataReader readerUri = commandUri.ExecuteReader())
    {
        while (readerUri.Read())
        {
            uriReadString = readerUri[0].ToString();
            System.IO.File.WriteAllText(@"C:\Users\Nathan\Documents\Visual Studio 2013\Projects\MVCImageUpload\uploads\GetUriNow.txt", uriReadString);
            uriList.Add(uriReadString);
        }

    }

    for(int i = 0; i < readList.Count; i++)
        SendPushNotification(uriList[i], readList[i]);