如何从复选框列表向SQL数据库发送多个值?

时间:2019-04-02 14:25:36

标签: html asp.net

这是我的新手,我有一个包含多个值的复选框列表,而且如果选择了多个值,我也无法弄清楚将值发送到SQL数据库中的字段的属性是什么。

SQL数据库以我设置的方式接受数据,但是它仅接收对SelectedValue有意义的第一个选择,但是我不确定我可以使用什么属性来发送多个值。

<asp:CheckBoxList ID="dentalcare" runat="server" class="checkbox">
        <asp:ListItem>Emergency Only</asp:ListItem>
        <asp:ListItem>Routine Care</asp:ListItem>
        <asp:ListItem>Needs Early Dental Work</asp:ListItem>
        <asp:ListItem>Infrequent Visits</asp:ListItem>
        <asp:ListItem>None</asp:ListItem>      
    </asp:CheckBoxList>




protected MedicalSave collectFormData()
    {
        MedicalSave medicalSave = new MedicalSave();
        medicalSave.Dentalcare = dentalcare.SelectedValue; 
        return medicalSave;
    }




public class MedicalSave
 {

    private string dentalcare;

    public string Dentalcare
    {
        get
        {
            return dentalcare;
        }
        set
        {
            dentalcare = value;
        }
    }
}




private static string GetConnectionString()
    {
        return 
 ConfigurationManager.ConnectionStrings 
["MySQLConnectionString"].ConnectionString;
    }

    public void SaveMedicalSave(Objects.MedicalSave MedicalSave)
    {
        var cmd = new SqlCommand("SaveMedicalDental", new 
SqlConnection(GetConnectionString()));
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Connection.Open();
        cmd.Parameters.AddWithValue("@dentalcare", 
MedicalSave.Dentalcare);
      cmd.ExecuteNonQuery();
    }

0 个答案:

没有答案