为了从Sql server数据库中读取数据,我被告知必须将读者放在using语句中。还有一个问题Read data from SqlDataReader与此类似,但它对我没什么帮助,因为我已经知道如何读取数据了。我只是不知道如何将数据放入函数中。问题是我需要使用两个不同using语句中的每个数据,我需要将它们放入相同的函数SendPushNotification()中。但是,每次使用一个阅读器时,我都会丢失数据。一个using语句将数据添加到
string readerTest;
一个using语句将数据添加到
string uriReadString;
然后我继续将两个作为参数传递给我的函数:
SendPushNotification(uriReadString, readerTest);
当他们都能够进入这个功能时,读者已经结束了,我的字符串又变回空洞了。 Plz帮助。
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);
}
答案 0 :(得分:0)
我相信这是你想要做的......如果不是,我会删除答案。手动将字符串添加到列表中,然后以并行方式,按顺序发送通知。
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]);