In order to read data from a Sql server database, I've been told that I must put the readers inside of using statements. There is another question Read data from SqlDataReader similar to this, but it didn't help me much, because I already knew how to read the data. I just didn't know how to put the data in the function. The problem is that I need to use each piece of data that is inside the two different using statements, and I need to put them into the same function SendPushNotification(). Every time one reader is done being used, though, I lose the data. One using statement adds the data to
string readerTest;
And one using statement adds the data to
string uriReadString;
I then proceed to pass both as arguments into my function:
SendPushNotification(uriReadString, readerTest);
By the time they are both able to be put into the function, the readers have ended, and my strings go back to being empty. Plz help.
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);
}
I believe this is what you're attempting to do... If it is not, I'll delete the answer. Add the string manually to a list and then in a parallel fashion, PushSend the notification respectfully.
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]);