从单独的using语句传递两个参数到函数
本文关键字:两个 参数 函数 单独 using 语句 | 更新日期: 2023-09-27 18:04:55
为了从Sql server数据库中读取数据,我被告知必须将读取器放在using语句中。还有一个问题,从SqlDataReader读取数据类似于此,但它没有帮助我太多,因为我已经知道如何读取数据。我只是不知道怎么把数据放进函数里。问题是我需要使用两个不同using语句中的每个数据块,并且我需要将它们放入同一个函数SendPushNotification()中。但是,每次使用完一个阅读器,我就会丢失数据。一个using语句将数据添加到
string readerTest;
一个using语句将数据添加到
string uriReadString;
然后将它们作为参数传递给函数:
SendPushNotification(uriReadString, readerTest);
当它们都能够被放入函数中时,读取器已经结束,我的字符串返回为空。请帮助。
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]);