C# 列表框| SqlReader中的每个项
我在这里要做的是选择有联系的人, 但我真的不知道怎么做。我在列表框中有C# 列表框| SqlReader中的每个项,c#,sql,listbox,C#,Sql,Listbox,我在这里要做的是选择有联系的人, 但我真的不知道怎么做。我在列表框中有x名称。 我想检查每个名字的登录和注销时间,如果登录比注销时间大,它会在列表框的“已连接”、“未连接”附近键入。 先谢谢你 foreach (var Item in listBoxControl2.Items) { try { SqlConnection sqlConnection = new SqlConnection(ConnectDatabase); SqlCommand
x
名称。
我想检查每个名字的登录和注销时间,如果登录比注销时间大,它会在列表框的“已连接”、“未连接”附近键入。
先谢谢你
foreach (var Item in listBoxControl2.Items)
{
try
{
SqlConnection sqlConnection = new SqlConnection(ConnectDatabase);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "Select login_time_value,logout_time_value ConnectionTime.dbo.Avalaible where name = '" + Item.ToString() +"'";
sqlConnection.Open();
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
while (true)
{
bool flag = sqlDataReader.Read();
if (!flag)
{
break;
}
DateTime login = sqlDataReader.GetDateTime(0);
DateTime logout = sqlDataReader.GetDateTime(1);
if (login > logout)
{
}
else
{
}
}
sqlDataReader.Close();
sqlConnection.Close();
}
catch
{
}
}
代码中有许多内容可以更改,但为了解决您的问题,我将更改循环以使用简单的for循环,这样您就可以直接访问列表框中的项目,并更改匹配项目的文本
for(x = 0; x < listBoxControl2.Items.Count; x++)
{
while(sqlDataReader.Read())
{
DateTime login = sqlDataReader.GetDateTime(0);
DateTime logout = sqlDataReader.GetDateTime(1);
if (login > logout)
{
listBoxControl2.Items[x] = listBoxControl2.Items[x] + " connected";
}
else
{
listBoxControl2.Items[x] = listBoxControl2.Items[x] + " logged off";
}
}
}
(x=0;x
{
while(sqlDataReader.Read())
{
DateTime login=sqlDataReader.GetDateTime(0);
DateTime logout=sqlDataReader.GetDateTime(1);
如果(登录>注销)
{
listBoxControl2.Items[x]=listBoxControl2.Items[x]+“已连接”;
}
其他的
{
listBoxControl2.Items[x]=listBoxControl2.Items[x]+“注销”;
}
}
}
foreach的问题是,您获得了字符串文本的副本,必须替换原始文本,而使用for循环更容易实现
关于其他问题
- 命令文本中的FROM子句在哪里
- 将连接的开口移到回路外部
- 使用using语句打开/使用/关闭/处置一次性文件 对象()
- 在生成要传递到的命令文本时使用参数化查询 数据库引擎()
string cmdText = "Select login_time_value,logout_time_value ConnectionTime.dbo.Avalaible " +
"FROM ??????????" +
"where name = @name";
using(SqlConnection sqlConnection = new SqlConnection(ConnectDatabase))
using(SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection))
{
sqlCommand.Parameters.AddWithValue("@name", "dummy");
sqlConnection.Open();
for(x = 0; x < listBoxControl2.Items.Count; x++)
{
string name = listBoxControl2.Items[x].ToString();
sqlCommand.Parameters["@name"].Value = name;
using(SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
while(sqlDataReader.Read())
{
DateTime login = sqlDataReader.GetDateTime(0);
DateTime logout = sqlDataReader.GetDateTime(1);
if (login > logout)
{
listBoxControl2.Items[x] = name + " connected";
}
}
}
}
}
string cmdText=“选择登录\u时间\u值,注销\u时间\u值ConnectionTime.dbo.available”+
“从…”
“其中name=@name”;
使用(SqlConnection SqlConnection=newsqlconnection(ConnectDatabase))
使用(SqlCommand-SqlCommand=new-SqlCommand(cmdText,sqlConnection))
{
sqlCommand.Parameters.AddWithValue(“@name”,“dummy”);
sqlConnection.Open();
对于(x=0;x注销)
{
listBoxControl2.Items[x]=名称+“已连接”;
}
}
}
}
}
要得到答案,您需要问一个问题。我没有问一个问题?:请正确格式化您的源代码。这次我是为你做的。你尝试过解决问题的方法吗?事实上,是的,我尝试过做类似的事情:string text++=Item.ToString()+“\n”;但是没有真正起作用…看看更新,现在该项直接从循环中当前位置的listBox items集合中获取。我已经删除了IF的else部分,如果在注销>登录的情况下也必须更新else,则需要else。可以使用>操作符比较两个日期时间,没有问题。您是否尝试使用调试器检查代码流?很抱歉。它起作用了,我只是改变了一些东西,所以。。。再次感谢你