数据列表 ASP.net c# 循环
本文关键字:循环 net ASP 列表 数据 | 更新日期: 2023-09-27 18:31:23
我有一个数据列表,它从包含服务器名称的SQL数据库中提取数据。(6 的 )
我想遍历数据列表,并为列表中的每个服务器使用 WMI 连接到它,看看值是否为真。
我有一个服务器名称,但当我将其与数据列表组合时则不然。
我哪里出错了?
谢谢
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;
}
}
}
一旦我将数据源绑定到数据列表,我就会调用此函数。
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
我必须接近吗?
更改:
SARHServerName = ((Label) (item.FindControl("ServerNameLabel"))).Text;