我有一个 Datalist,它从包含服务器名称的 SQL 数据库中提取数据。(6 个)
我想遍历 Datalist 并为列表中的每个服务器使用 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;
}
}
}
将数据源绑定到 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
我一定很近吧?