SQL 行作为复选框
SQL Rows as CheckBoxes
我有一个员工 Web 表单,可以在其中将员工分配到任意数量的市场。我正在尝试获取 SQL 行值并将它们分配给 C# 中的复选框。
例如,我的 SQL 数据库将如下所示:
EmployeeID MarketID
99999 1
99999 2
99999 4
我后面的代码是
string sqlConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection sqlConnection1 = new SqlConnection(sqlConn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = ("usp_EmployeeMarkets");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValue("EmployeeID", id);
sqlConnection1.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Market1CheckBox.Checked = sdr["MarketID"].Equals(1);
Market2CheckBox.Checked = sdr["MarketID"].Equals(2);
Market3CheckBox.Checked = sdr["MarketID"].Equals(3);
Market4CheckBox.Checked = sdr["MarketID"].Equals(4);
}
}
}
所以在上面的例子中,复选框 1、2、4 应该被选中,而复选框 3 不应该被选中。显然这是不正确的,因为我已经检查了 none 个复选框。我怎样才能完成我想要做的事情?
这是我最后做的事情:
string sqlConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection sqlConnection1 = new SqlConnection(sqlConn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = ("usp_EmployeeMarkets");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValue("EmployeeID", id);
sqlConnection1.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string value = sdr["MarketID"].ToString();
if (value == "1")
{
Market1CheckBox.Checked = true;
}
if (value == "2")
{
Market2CheckBox.Checked = true;
}
if (value == "3")
{
Market3CheckBox.Checked = true;
}
if (value == "4")
{
Market4CheckBox.Checked = true;
}
}
}
}
List<string> listOfMarkets=new List<string>();
while (sdr.Read())
{
listOfMarkets.Add(sdr["MarketID"].ToString());
}
Market1CheckBox.Checked = listOfMarkets.Contains("1")?true:false;
Market2CheckBox.Checked = listOfMarkets.Contains("2")?true:false;
Market3CheckBox.Checked = listOfMarkets.Contains("3")?true:false;
Market4CheckBox.Checked =listOfMarkets.Contains("4")?true:false;
试试这个肯定有用
我有一个员工 Web 表单,可以在其中将员工分配到任意数量的市场。我正在尝试获取 SQL 行值并将它们分配给 C# 中的复选框。
例如,我的 SQL 数据库将如下所示:
EmployeeID MarketID
99999 1
99999 2
99999 4
我后面的代码是
string sqlConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection sqlConnection1 = new SqlConnection(sqlConn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = ("usp_EmployeeMarkets");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValue("EmployeeID", id);
sqlConnection1.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Market1CheckBox.Checked = sdr["MarketID"].Equals(1);
Market2CheckBox.Checked = sdr["MarketID"].Equals(2);
Market3CheckBox.Checked = sdr["MarketID"].Equals(3);
Market4CheckBox.Checked = sdr["MarketID"].Equals(4);
}
}
}
所以在上面的例子中,复选框 1、2、4 应该被选中,而复选框 3 不应该被选中。显然这是不正确的,因为我已经检查了 none 个复选框。我怎样才能完成我想要做的事情?
这是我最后做的事情:
string sqlConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection sqlConnection1 = new SqlConnection(sqlConn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = ("usp_EmployeeMarkets");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValue("EmployeeID", id);
sqlConnection1.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
string value = sdr["MarketID"].ToString();
if (value == "1")
{
Market1CheckBox.Checked = true;
}
if (value == "2")
{
Market2CheckBox.Checked = true;
}
if (value == "3")
{
Market3CheckBox.Checked = true;
}
if (value == "4")
{
Market4CheckBox.Checked = true;
}
}
}
}
List<string> listOfMarkets=new List<string>();
while (sdr.Read())
{
listOfMarkets.Add(sdr["MarketID"].ToString());
}
Market1CheckBox.Checked = listOfMarkets.Contains("1")?true:false;
Market2CheckBox.Checked = listOfMarkets.Contains("2")?true:false;
Market3CheckBox.Checked = listOfMarkets.Contains("3")?true:false;
Market4CheckBox.Checked =listOfMarkets.Contains("4")?true:false;
试试这个肯定有用