【发布时间】:2015-06-18 20:57:26
【问题描述】:
我将表单数据发送到 sql 数据库,并以另一种形式显示我的用户的输出。我刚刚添加了一个复选框字段,并将值设置为“Y”和“N”以表示是和否。我能够保存它们并检索它,但我试图将该值绑定到表单中的复选框字段。
所以我的问题是如何在我的复选框字段中显示数据库中该特定项目的字段值?
这是我目前所拥有的,但没有奏效:
private void LoadData()
{
Connection connection = new Connection();
try
{
connection.connection1();
SqlCommand com = new SqlCommand("Select Image,UserName,Description,Private,CompValue FROM FutureProjTbl Where id='" + projectId + "'", connection.con);
SqlDataReader dataReader = com.ExecuteReader();
while (dataReader.Read())
{
image1.Src = dataReader["Image"].ToString();
Owner.Text = dataReader["UserName"].ToString();
Description.Text = dataReader["Description"].ToString();
//The following 2 are my attempts to retrieve the item and bind it to the checkbox on the project template all the values will load into.
//privateItem.Checked = Convert.ToBoolean(dataReader["Private"].ToString());
//privateItem = (CheckBox) dataReader["Private"];
compValue.Text = dataReader["CompValue"].ToString();
FillDataForEditProjectModal(dataReader);
}
dataReader.Close();
}
catch (Exception)
{
throw;
}
}
【问题讨论】:
-
你应该考虑使用像实体框架这样的 ORM,当涉及到这样的操作时,它会让你的生活变得更轻松。