在检查的数据库中存储多个值

时间:2017-02-01 23:22:22

标签: c# asp.net sql-server

我有一个复选框列表。

<asp:CheckBoxList ID="cbBowelSounds" runat="server">
    <asp:ListItem Text="&nbsp;&nbsp;<span style=font-weight:normal;>1 Quadrant</span>" Value="1 Quadrant" />
    <asp:ListItem Text="&nbsp;&nbsp;<span style=font-weight:normal;>2 Quadrant</span>" Value="2 Quadrant" />
    <asp:ListItem Text="&nbsp;&nbsp;<span style=font-weight:normal;>Hypo</span>" Value="Hypo" />
    <asp:ListItem Text="&nbsp;&nbsp;<span style=font-weight:normal;>Hyper</span>" Value="Hyper" />
    <asp:ListItem Text="&nbsp;&nbsp;<span style=font-weight:normal;>Normal</span>" Value="Normal" />
</asp:CheckBoxList>

您可以选择多个值。我想将这些值存储在SQL Server数据库中。

起初我以为我会在SQL Server表中创建5列,然后将每个选定的值添加到正确的列中。那没用。

然后我找到了这个片段:

var s = cbBowelSounds.SelectedValue;
string[] values = s.Split(',');

foreach (ListItem item in cbBowelSounds.Items) 
     item.Selected = values.Contains(item.Value);

这很棒......直到数据库报告了这个答案:System.String[]

那么,我能在这做什么?它是一个具有多个值的列还是第一个列表项写入第1列的5列,等等,这与我无关。我只需要选择要发送到DB的值。

谢谢

我正在将它用于SQL代码

  private void InsertPatientNote()
  {
     var patId = txtPatId.Text;
     string selectedValue = rbVitalsTaken.SelectedValue;

     var s = cbBowelSounds.SelectedValue;
     string[] values = s.Split(',');
     bool quadOne = values.Contains("1 Quadrant");
     bool quadTwo = values.Contains("2 Quadrant");
     bool hypo = values.Contains("Hypo");
     bool hyper = values.Contains("Hyper");
     bool normal = values.Contains("Normal");

     var strConnString = ConfigurationManager.ConnectionStrings["HBMConnectionString"].ConnectionString;

     SqlConnection con = new SqlConnection(strConnString);

     SqlCommand command = new SqlCommand();
     command.CommandType = CommandType.StoredProcedure;
     command.CommandText = "uspInsertPatientNote";

     command.Parameters.Add("@patId", SqlDbType.VarChar).Value = patId;
     command.Parameters.Add("@rnId", SqlDbType.VarChar).Value = txtNurseId.Text.Trim();
     command.Parameters.Add("@PhysName", SqlDbType.VarChar).Value = txtPhysName.Text.Trim();
     command.Parameters.Add("@visitDate", SqlDbType.VarChar).Value = tbVisitDate.Text.Trim();
     command.Parameters.Add("@visitTimeInHr", SqlDbType.VarChar).Value = txtHourIn.Text.Trim();
     command.Parameters.Add("@visitTimeInMin", SqlDbType.VarChar).Value = txtMinuteIn.Text.Trim();
     command.Parameters.Add("@visitTimeInAmPm", SqlDbType.VarChar).Value = txtAmPmIn.Text.Trim();
     command.Parameters.Add("@visitTimeOutHr", SqlDbType.VarChar).Value = txtTimeOutHr.Text.Trim();
     command.Parameters.Add("@visitTimeOutMin", SqlDbType.VarChar).Value = txtTimeOutMin.Text.Trim();
     command.Parameters.Add("@visitTimeOutAmPm", SqlDbType.VarChar).Value = txtAmPmOut.Text.Trim();
     command.Parameters.Add("@extraPay", SqlDbType.VarChar).Value = rbExtraPayAmount.SelectedValue;
     command.Parameters.Add("@extraPayAmt", SqlDbType.VarChar).Value = txtExtraPayAmount.Text.Trim();
     command.Parameters.Add("@patIdentify", SqlDbType.VarChar).Value = rbPatIdent.SelectedValue;
     command.Parameters.Add("@careGiverAvail", SqlDbType.VarChar).Value = rbCaregiverAvail.SelectedValue;
     command.Parameters.Add("@careGiverAssist", SqlDbType.VarChar).Value = txtCaregiverHelper.Text.Trim();
     command.Parameters.Add("@VitalsTaken", SqlDbType.VarChar).Value = selectedValue;
     command.Parameters.Add("@BodyTemp", SqlDbType.VarChar).Value = txtVitalBodyTemp.Text.Trim(); //Label290.Text;
     command.Parameters.Add("@PulseRate", SqlDbType.VarChar).Value = txtVitalPusleRate.Text.Trim();
     command.Parameters.Add("@RespRate", SqlDbType.VarChar).Value = txtVitalRespRate.Text.Trim();
     command.Parameters.Add("@BloodPressure", SqlDbType.VarChar).Value = txtVitalBloodPress.Text.Trim();
     command.Parameters.Add("@WtTaken", SqlDbType.VarChar).Value = rbWeightTaken.SelectedValue;
     command.Parameters.Add("@ActaulWt", SqlDbType.VarChar).Value = txtInputActualWeigth.Text.Trim();
     command.Parameters.Add("@StatedWt", SqlDbType.VarChar).Value = txtInputStatedWeight.Text.Trim();
     command.Parameters.Add("@Stability", SqlDbType.VarChar).Value = rbStabilityWeight.SelectedValue;
     command.Parameters.Add("@VitalComments", SqlDbType.VarChar).Value = txtVitalComments.Text.Trim();
     command.Parameters.Add("@LungSounds", SqlDbType.VarChar).Value = rbLungsSound.SelectedValue;
     command.Parameters.Add("@LungSoundsOther", SqlDbType.VarChar).Value = txtOthersLungSound.Text.Trim();
     command.Parameters.Add("@Sob", SqlDbType.VarChar).Value = rbShortnessBreath.SelectedValue;
     command.Parameters.Add("@SobDescribe", SqlDbType.VarChar).Value = rbSobDesc.SelectedValue;
     command.Parameters.Add("@SobOther", SqlDbType.VarChar).Value = txtSobOther.Text.Trim();
     command.Parameters.Add("@Sputum", SqlDbType.VarChar).Value = txtSputum.Text.Trim();
     command.Parameters.Add("@Nebulizer", SqlDbType.VarChar).Value = txtNebDrug.Text.Trim();
     command.Parameters.Add("@Oxygen", SqlDbType.VarChar).Value = txtOxygen.Text.Trim();
     command.Parameters.Add("@Continuous", SqlDbType.VarChar).Value = rbOxyContinuous.SelectedValue;
     command.Parameters.Add("@Prn", SqlDbType.VarChar).Value = rbOxyPrn.SelectedValue;
     command.Parameters.Add("@Nasal", SqlDbType.VarChar).Value = rbOxyNasal.SelectedValue;
     command.Parameters.Add("@Trach", SqlDbType.VarChar).Value = rbOxyTrach.SelectedValue;
     command.Parameters.Add("@RespComments", SqlDbType.VarChar).Value = txtRespComments.Text.Trim();
     command.Parameters.Add("@Rhythm", SqlDbType.VarChar).Value = rbCardioRhythm.SelectedValue;
     command.Parameters.Add("@Symptoms", SqlDbType.VarChar).Value = rbCardioSymp.SelectedValue;
     command.Parameters.Add("@SymptomsOther", SqlDbType.VarChar).Value = txtOtherCardioSymp.Text.Trim();
     command.Parameters.Add("@Edema", SqlDbType.VarChar).Value = rbEdema.SelectedValue;
     command.Parameters.Add("@Rle", SqlDbType.VarChar).Value = txtRle.Text.Trim();
     command.Parameters.Add("@Rue", SqlDbType.VarChar).Value = txtRue.Text.Trim();
     command.Parameters.Add("@Lle", SqlDbType.VarChar).Value = txtLle.Text.Trim();
     command.Parameters.Add("@Lue", SqlDbType.VarChar).Value = txtLue.Text.Trim();
     command.Parameters.Add("@EdemaComments", SqlDbType.VarChar).Value = txtEdemaComments.Text.Trim();
     command.Parameters.Add("@BowelMovements", SqlDbType.VarChar).Value = rbBowelMove.SelectedValue;
     command.Parameters.Add("@LastBowelMove", SqlDbType.VarChar).Value = txtLastBowelMove.Text.ToString();
     command.Parameters.Add("@BowelSounds1", SqlDbType.VarChar).Value = quadOne;
     command.Parameters.Add("@BowelSounds2", SqlDbType.VarChar).Value = quadTwo;
     command.Parameters.Add("@BowelSounds3", SqlDbType.VarChar).Value = hypo;
     command.Parameters.Add("@BowelSounds4", SqlDbType.VarChar).Value = hyper;
     command.Parameters.Add("@BowelSounds5", SqlDbType.VarChar).Value = normal;
     command.Parameters.Add("@Dialysis", SqlDbType.VarChar).Value = rbDialysisYn.SelectedValue;
     command.Parameters.Add("@Treatment", SqlDbType.VarChar).Value = rbDialysis.SelectedValue;
     command.Parameters.Add("@Urine", SqlDbType.VarChar).Value = rbUrine.SelectedValue;
     command.Parameters.Add("@UrineOther", SqlDbType.VarChar).Value = tbUrine.Text.Trim();
     command.Parameters.Add("@BowelComments", SqlDbType.VarChar).Value = tbEmulationComments.Text.Trim();

     command.Connection = con;

     try
     {
        con.Open();
        command.ExecuteNonQuery();
        // lblMessage.Text = "Record inserted successfully";
     }
     catch (Exception ex)
     {
        throw ex;
     }
     finally
     {
        con.Close();
        con.Dispose();
     }
  }

数据库在表

中显示这些值
  • BowelSounds1 True
  • BowelSounds2 False
  • BowelSounds3 False
  • BowelSounds4 False
  • BowelSounds5 False

然而,在页面上检查1(1象限),3(Hypo)和5(正常)。

1 个答案:

答案 0 :(得分:0)

我可以看到这很简单,它使用你之前提供的块来将值放入字符串数组中:

var s = cbBowelSounds.SelectedValue;
string[] values = s.Split(',');

bool 1Quadrant = values.Contains("1 Quadrant");
bool 2Quadrant = values.Contains("2 Quadrant"); // Edit to fix this, was previously 1 Quadrant
bool hypo = values.Contains("Hypo");
bool hyper = values.Contains("Hyper");
bool normal = values.Contains("Normal");

command.Parameters.Add("@1Quadrant", SqlDbType.Boolean).Value = 1Quadrant;         
command.Parameters.Add("@2Quadrant", SqlDbType.Boolean).Value = 2Quadrant;
command.Parameters.Add("@Hypo", SqlDbType.Boolean).Value = hypo;
 ...etc...

这可能符合您的需求?祝你好运!