public class WebRole : RoleEntryPoint
{
public struct countries
{
public const double usa = 94.2;
public const double canada = 85.63;
}
public List<Tuple<string, string, double>> people = new List<Tuple<string, string, double>>(); //table, userID, distance
string[] connarr = { "SELECT UserID FROM [0971880107_0];", "SELECT UserID FROM [0971880107_1];" };
public override bool OnStart()
{
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
// sqlconn();
foreach (string s in connarr)
{
get_loc_age(retrieve_value(s) , s); //s =table name, return users in table as list
}
return base.OnStart();
}
public List<string> retrieve_value(string s) //returning users for "Item A Eval X"
{
string connectionString = null;
connectionString = @"Data Source=192.168.1.222, 1433;Initial Catalog=dbs1;User ID=pratikkcn;Password=abc123";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);
SqlCommand comm = new SqlCommand(s, cnn);
try
{
cnn.Open();
SqlDataReader reader = comm.ExecuteReader();
List<string> str = new List<string>();
int i = 0;
while (reader.Read())
{
str.Add(reader.GetValue(0).ToString().Trim(new Char[] { '"' }));
}
reader.Close();
return str;
}
catch (Exception)
{
throw;
}
finally
{
cnn.Close();
}
}
public void get_loc_age( List<string> str , string s)
{
string connectionString = null;
connectionString = @"Data Source=192.168.1.222, 1433;Initial Catalog=book_rating_dataset;User ID=pratikkcn;Password=abc123";
SqlConnection cnn;
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
foreach (var userid in str)
{
SqlCommand comm = new SqlCommand("SELECT Location, Age FROM [Users_cleaned] where UserId = '"+ userid +"'" , cnn);
double[] dummy = { 5.0, 5.0 }; // dummy variable
SqlDataReader reader = comm.ExecuteReader();
double p= 0.0 ,a = 0.0;
string l = "";
while (reader.Read())
{
if (reader.IsDBNull(0))
{
l = null;//location
a = 0.0;
}
else
{
l = reader.GetString(0);
a = Convert.ToDouble(reader.GetString(1)); //age
}
#region countries_switch
switch (l)
{
case "usa":
p = countries.usa;
break;
case "canada":
p = countries.canada;
break;
}
#endregion
}
reader.Close();
double[] x1 = { a, p };
double x2 = EuclideanDistance(x1, dummy); //distance
people.Add(new Tuple<string, string, double>(s, userid, x2));
var c = people.OrderBy(x => x.Item3).ToList(); //show properly ordered list here
}
var c = people.OrderBy(x => x.Item3).ToList(); //c get null here
}
catch (Exception)
{
throw;
}
finally
{
cnn.Close();
}
}
在函数get_loc_age()
的foreach循环结束后,我想获得c的值,但它显示null
。但是,如果我试图在foreach中获得c,它显示价值很好。此代码是从webrole的start方法调用的。我究竟做错了什么。任何线索。