DataList ASP.net c#循环播放

时间:2013-09-16 10:06:30

标签: c# asp.net datalist

我有一个Datalist,它从包含Servers Names的SQL数据库中提取数据。 (6)

我想遍历Datalist,并且列表中的每个服务器都使用WMI连接到它,并查看值是否为真。

我有一个服务器名称,但是当我将它与datalist结合使用时。

我哪里错了?

由于

 string SARHServerName = "";
 //SARHServerName = reader["ServerName"].ToString();

 ConnectionOptions con = new ConnectionOptions();
 con.Username = "Username";
 con.Password = "Password";

 foreach (DataListItem item in DataList.Items)
 {

      Label1.Text = SARHServerName;
      SARHServerName += "," + ((Label)    (item.FindControl("ServerNameLabel"))).Text;
      ManagementScope scope =
        new ManagementScope(
        "\\\\" + SARHServerName + "\\root\\CIMV2\\TerminalServices", con);
      scope.Options.EnablePrivileges = true;
      scope.Options.Authentication = AuthenticationLevel.PacketPrivacy;
      scope.Options.Impersonation = ImpersonationLevel.Impersonate;
      scope.Connect();

      //create object query
      ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalServiceSetting");

      //create object searcher
      ManagementObjectSearcher searcher =
                                            new ManagementObjectSearcher(scope,     query);

      //get collection of WMI objects
      ManagementObjectCollection queryCollection = searcher.Get();

      //enumerate the collection.
      foreach (ManagementObject m in queryCollection)
      {
          if (m["Logons"].ToString() == "0")
          {
              ((Image)(item.FindControl("ServerDown"))).Visible = true;
              ((Image)(item.FindControl("ServerUP"))).Visible = false;
          }
          else
          {
              ((Image)(item.FindControl("ServerDown"))).Visible = false;
              ((Image)(item.FindControl("ServerUP"))).Visible = false;
          }
       }
  }

我将数据源绑定到Datalist后调用此函数。

public void GetDataFromSQLGridView()
{

SqlConnection conn = null;

    try
     {

conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DailyChecksConnectionString"].ConnectionString);

            {
                conn.Open();
                using (SqlCommand cmd =
                    new SqlCommand("SELECT ServerName FROM Servers_Checks", conn))
                {

                    DataListSQL.DataSource = cmd.ExecuteReader();
                    DataListSQL.DataBind();
                }

            }

        }
        catch { }
        finally { GetRemoteDesktop(); }


}

这是我的SQL命令吗?看起来它一次性从SQL返回所有服务器名称,而不是返回一个然后继续并拉回另一条记录。

好的,我已经找到了一些关于此的更多信息。我做了一个

Response.Write(SARHServerName);
Response.Write("<br />");

它通过循环很好,但它连接像这样的结果

server1
server1server2
server1server2server3
server1server2server3server4
server1server2server3server4server5
server1server2server3server4server5server6

我必须亲近?

1 个答案:

答案 0 :(得分:0)

变化:

SARHServerName = ((Label)    (item.FindControl("ServerNameLabel"))).Text;