复选框列表,如果 mysql 中的状态为真,则必须检查名称
Checkboxlist if status is true in mysql the name must be checked
我有两列,一列是 checkStatus,另一列是 EmployeeName
我已经得到了所有的名字,但我不知道如何得到 checkStatus 如果状态为真则必须检查它,如果不是则不检查
string Query = "Select Distinct EmployeeName From tblschedule Where ScheduleFrom BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' "; ;
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
try
{
conn.Open();
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
checkedListBox1.Items.Add(employeeName);
}
}
}
这是我获取名字的代码..
string Query = "Select Distinct EmployeeName,checkStatus From tblschedule Where ScheduleFrom
BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' "; ;
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
try
{
conn.Open();
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
string checkStatus= (myReader.IsDBNull(myReader.GetOrdinal("checkStatus")) ?
string.Empty : myReader["checkStatus"].ToString());
checkedListBox1.Items.Add(employeeName);
if(checkStatus=="true")
{ checkedListBox1.SetItemChecked(checkedListBox1.Items.Count-1, true);
}
}
}
}
根据您的描述,您可以修改查询以包含状态吗?
您可能还应该参数化您的查询。
您的查询的外观示例:
string Query = "SELECT EmployeeName, checkStatus FROM tblschedule WHERE ScheduleFrom BETWEEN @StartDate AND @EndDate ";
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
cmd.Parameters.AddWithValue("@StartDate", (MySqlDbType.DateTime)clsSQLcon.DateFrom1);
cmd.Parameters.AddWithValue("@EndDate", (MySqlDbType.DateTime)clsSQLcon.DateTo1);
conn.Open();
try{
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = myReader.IsDBNull(0) ? string.Empty : myReader.GetString(0);
bool cstatus = myReader.GetBoolean(1);
//do something with name and status and check distinct names in code instead
}
}
}
catch(MySqlException){//Exception handling}
}
如果您只需要不同的名称,您可以在查询中添加“Group by (EmployeeName)”,但如果时间范围内有多个条目,您将随机获得一个。
@Jonjon Candare。
你可以尝试参考我的代码。我在数据库中将 checkStatus 设置为 BIT 类型。在查询语句中加入checkStatus,读取数据时读取checkStatus。然后进行判断,设置checkedListBox1的SetItemChecked。
我的cs代码如下:
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Connstr";
MySqlConnection con = new MySqlConnection(constr);
string query = " Select Distinct EmployeeName,checkStatus From tblschedule Where ScheduleFrom BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' " ";
con.Open();
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
try
{
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
string checkStatus = (myReader.IsDBNull(myReader.GetOrdinal("checkStatus")) ?
string.Empty : myReader["checkStatus"].ToString());
checkedListBox1.Items.Add(employeeName);
if (checkStatus == "True")
{
checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, true);
}
if (checkStatus == "False")
{
checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, false);
}
}
}
}
Catch(MySqlException)
{
}
}
}
结果如图:
我有两列,一列是 checkStatus,另一列是 EmployeeName
我已经得到了所有的名字,但我不知道如何得到 checkStatus 如果状态为真则必须检查它,如果不是则不检查
string Query = "Select Distinct EmployeeName From tblschedule Where ScheduleFrom BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' "; ;
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
try
{
conn.Open();
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
checkedListBox1.Items.Add(employeeName);
}
}
}
这是我获取名字的代码..
string Query = "Select Distinct EmployeeName,checkStatus From tblschedule Where ScheduleFrom
BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' "; ;
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
try
{
conn.Open();
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
string checkStatus= (myReader.IsDBNull(myReader.GetOrdinal("checkStatus")) ?
string.Empty : myReader["checkStatus"].ToString());
checkedListBox1.Items.Add(employeeName);
if(checkStatus=="true")
{ checkedListBox1.SetItemChecked(checkedListBox1.Items.Count-1, true);
}
}
}
}
根据您的描述,您可以修改查询以包含状态吗?
您可能还应该参数化您的查询。
您的查询的外观示例:
string Query = "SELECT EmployeeName, checkStatus FROM tblschedule WHERE ScheduleFrom BETWEEN @StartDate AND @EndDate ";
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
cmd.Parameters.AddWithValue("@StartDate", (MySqlDbType.DateTime)clsSQLcon.DateFrom1);
cmd.Parameters.AddWithValue("@EndDate", (MySqlDbType.DateTime)clsSQLcon.DateTo1);
conn.Open();
try{
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = myReader.IsDBNull(0) ? string.Empty : myReader.GetString(0);
bool cstatus = myReader.GetBoolean(1);
//do something with name and status and check distinct names in code instead
}
}
}
catch(MySqlException){//Exception handling}
}
如果您只需要不同的名称,您可以在查询中添加“Group by (EmployeeName)”,但如果时间范围内有多个条目,您将随机获得一个。
@Jonjon Candare。 你可以尝试参考我的代码。我在数据库中将 checkStatus 设置为 BIT 类型。在查询语句中加入checkStatus,读取数据时读取checkStatus。然后进行判断,设置checkedListBox1的SetItemChecked。
我的cs代码如下:
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Connstr";
MySqlConnection con = new MySqlConnection(constr);
string query = " Select Distinct EmployeeName,checkStatus From tblschedule Where ScheduleFrom BETWEEN '"+clsSQLcon.DateFrom1+"' AND '"+ clsSQLcon.DateTo1 + "' " ";
con.Open();
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
try
{
using (MySqlDataReader myReader = cmd.ExecuteReader())
{
while (myReader.Read())
{
string employeeName = (myReader.IsDBNull(myReader.GetOrdinal("EmployeeName")) ?
string.Empty : myReader["EmployeeName"].ToString());
string checkStatus = (myReader.IsDBNull(myReader.GetOrdinal("checkStatus")) ?
string.Empty : myReader["checkStatus"].ToString());
checkedListBox1.Items.Add(employeeName);
if (checkStatus == "True")
{
checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, true);
}
if (checkStatus == "False")
{
checkedListBox1.SetItemChecked(checkedListBox1.Items.Count - 1, false);
}
}
}
}
Catch(MySqlException)
{
}
}
}
结果如图: