C# I';我正在尝试向数据库中写入复选框列表,我遗漏了什么?
我是C#的新手,我认为我还没有很好地掌握如何写入数据库。我不知道如何将复选框写入数据库中的特定列。如果你能看到我需要特别研究的领域,请告诉我。很难知道我需要知道什么来教自己如何做这样的事情。谢谢你的帮助 如果你需要任何进一步的信息,请告诉我 目标:我的目标是将复选框选择写入数据库中的表中。每个复选框在一个表中都有一列,如果选中某个内容,我只希望填充一个“Y”,如果未选中,则不填充任何内容。列的标题与复选框标签相同 问题:C# I';我正在尝试向数据库中写入复选框列表,我遗漏了什么?,c#,asp.net,C#,Asp.net,我是C#的新手,我认为我还没有很好地掌握如何写入数据库。我不知道如何将复选框写入数据库中的特定列。如果你能看到我需要特别研究的领域,请告诉我。很难知道我需要知道什么来教自己如何做这样的事情。谢谢你的帮助 如果你需要任何进一步的信息,请告诉我 目标:我的目标是将复选框选择写入数据库中的表中。每个复选框在一个表中都有一列,如果选中某个内容,我只希望填充一个“Y”,如果未选中,则不填充任何内容。列的标题与复选框标签相同 问题: 我不知道如何将数据从复选框列表中获取到数据库中 DB结构: 网站草稿样本
我不知道如何将数据从复选框列表中获取到数据库中 DB结构: 网站草稿样本: HTML:
问题是您没有将数据写入数据库的代码 您需要遍历
chkbxlst1
中的项目,并设置适当的参数:
foreach(ListItem item in chkbxlst1.Items)
{
if(item.Checked)
sqlCom.Parameters.AddWithValue("@"+item.Text, "Y");
}
备注:
Y/N
。将列更改为使用位
类型foreach
中使用了对ListItem
的强制转换,因为我不确定CheckBoxList的Items
属性的类型您到底想向数据库写入什么?我没有看到任何试图从
chkbxlst1
获取值或将这些值放入该查询的尝试。如果您使用硬编码值手动编写这样的INSERT
会是什么样子?“我不知道如何将数据从复选框列表中获取到数据库中。”该过程包含多个步骤。首先,您必须知道选中了哪些复选框。接下来,您必须知道用于在数据库中存储此数据的表是如何设计的,或者,如果尚未设计,您必须了解系统其余部分将如何使用此数据来设计它们。最后,您必须知道如何在应用程序使用的表单中向数据库写入(以及稍后如何从数据库读取)数据。很好,没有让我们知道您的问题所在。@JM1:嗯,我们当然不知道您的数据库是什么样子,也不知道您想在其中插入什么数据。从头开始。您的数据库的结构是什么?调用INSERT
操作后,您希望那里有哪些数据?+1至少用于使用参数。良好的SQL注入保护。仍然需要“使用”语句…@JM1:Ah,现在我们有进展了。查看chkbxlst1
对象上的.Items
属性。还可以看看这里的一些代码示例:我建议使用bit
列来表示真正的布尔值,而不是varchar(50)
列中的“Y”
或“N”
。(如果只需要1个字符,为什么需要50个字符?)但基本上,您应该能够检查.Items
并查看哪些项目是“选中的”。您可以在那里设置SQL命令参数中的值。谢谢@RePierre。如果我将列的类型更改为bit类型,那将如何更改循环?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
namespace Revocations
{
public partial class Default_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Establish SQL Connection
SqlConnection sqlCon = new SqlConnection("Data Source = acctsql; Initial Catalog = PPSS_Work; Integrated Security = True");
{
// Write INSERT SQL statement
SqlCommand sqlCom = new SqlCommand("INSERT INTO [PPSS_Work].[Transfers].[t_Revocations](SchoolYear, Student_ID, Last_Name, First_Name, Revoked_School,Revocation_Date, Academics, Attendance, Behavior, Cooperative Relationship, Notes) VALUES(@SchoolYear, @Student_ID, @First_Name, @Last_Name, @Revoked_School, @Revocation_Date,@Academics, @Attendance, @Behavior, @Cooperative Relationship, @Notes)", sqlCon);
sqlCom.Parameters.AddWithValue("@SchoolYear", txtSchoolYear.Text);
sqlCom.Parameters.AddWithValue("@Student_ID", txtStudentID.Text);
sqlCom.Parameters.AddWithValue("@First_Name", txtFirstName.Text);
sqlCom.Parameters.AddWithValue("@Last_Name", txtLastName.Text);
sqlCom.Parameters.AddWithValue("@Revoked_School", txtRevokedSchool.Text);
sqlCom.Parameters.AddWithValue("@Revocation_Date", txtRevocationDate.Text);
sqlCom.Parameters.AddWithValue("@Notes", txtNotes.Text);
sqlCom.Parameters.AddWithValue("@Academics", txtNotes.Text);
sqlCom.Parameters.AddWithValue("@Attendance", txtNotes.Text);
sqlCom.Parameters.AddWithValue("@Behavior", txtNotes.Text);
sqlCom.Parameters.AddWithValue("@[Cooperative Relationship]", txtNotes.Text);
sqlCon.Open();
sqlCom.ExecuteNonQuery();
sqlCon.Close();
if (IsPostBack)
{
txtSchoolYear.Text = "";
txtStudentID.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtRevokedSchool.Text = "";
txtRevocationDate.Text = "";
chkbxlst1.Text = "";
txtNotes.Text = "";
}
}
}
}
}
foreach(ListItem item in chkbxlst1.Items)
{
if(item.Checked)
sqlCom.Parameters.AddWithValue("@"+item.Text, "Y");
}