背景:我一直在工作的asp.net網站(準系統客戶端管理系統)與C#代碼背後的文件,並建立基本表格插入新的數據和更新現有的數據在一個SQL數據庫中。檢查複選框,如果項目在數據表中
我有一個包含30+複選框(不在列表),它被用來確定什麼是「疾病」的患者表示,他們已經當我們第一次通過創建存儲過程的新病人記錄表(PatientMedicalHistory)。數據存儲爲組合主鍵(PatientId int + AilmentId int)以避免重複。潛在的與病症被存儲在另一個表(與病症),它保存的多個組合AilmentId INT和AilmentDescription VARCHAR(50)
我也許應該在1中使用的不同的模式(bit列用於每個病組合表),但這樣做會留下餘地,爲未來增加新的「疾病」。
問題:當使用SqlCommand的拉基於PatientId我想通過當前選擇的記錄列表進行迭代,並相應地檢查每個相應的複選框PatientMedicalHistroy臺一組的患者記錄。
CODE:
pIdstring = ("SELECT ailmentId FROM dbo.PatientMedicalHistory WHERE PatientId = " + pId.ToString());
cmd = new SqlCommand(pIdstring);
DataTable dt = new DataTable();
cmd.Connection = conn;
using (conn)
{
using (cmd)
{
conn.Open();
using (SqlDataReader drCbox = cmd.ExecuteReader())
{
dt.Load(drCbox);
//Need to check all checkboxes that are contained in this datatable.
}
}
}
}
HTML:
<fieldset class="auto-style93" style="background-image: url('Pictures/Bg_WhiteGrayGradient_Tall.jpg'); background-position: center; border-style: inset; border-width: medium">
<legend id="MedHistoryLegend" style="background-image: url('Pictures/Bg_WhiteGrayGradient_Tall.jpg'); border-style: inset; border-width: medium; font-family: 'Arial Black';">
<span class="auto-style194">MEDICAL HISTORY</span><asp:Button ID="EditHistory" runat="server" BackColor="White" BorderStyle="None" BorderWidth="0px" CssClass="auto-style189" Height="16px" Text="Edit" ToolTip="Edit Personal Information" OnClick="EditHistory_Click" />
</legend>
<table class="auto-style84" border="3">
<tr><td class="auto-style85">
<asp:CheckBox ID="AnemiaCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Anemia</td>
<td class="auto-style39">
<asp:CheckBox ID="EmphysemaCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Emphysema</td>
<td class="auto-style39">
<asp:CheckBox ID="HypoglycemiaCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hypoglycemia</td>
<td class="auto-style39">
<asp:CheckBox ID="ShinglesCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Shingles</td>
<td class="auto-style76">
<asp:CheckBox ID="UTICbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Urinary Tract Infection</td>
</tr>
<tr><td class="auto-style86">
<asp:CheckBox ID="ArthritisCbox" runat="server" Text=" " />
</td>
<td class="auto-style35" style="font-family: Arial, Helvetica, sans-serif">Arthritis</td>
<td class="auto-style31">
<asp:CheckBox ID="EpilepsyCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Epilepsy or Seizures</td>
<td class="auto-style39">
<asp:CheckBox ID="IrregularHBCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Irregular Heartbeat</td>
<td class="auto-style39">
<asp:CheckBox ID="SickleCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Sickle Cell Disease</td>
<td class="auto-style76">
<asp:CheckBox ID="JaundiceCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Yellow Jaundice</td>
</tr>
<tr><td class="auto-style86">
<asp:CheckBox ID="AsthmaCbox" runat="server" Text=" " />
</td>
<td class="auto-style35" style="font-family: Arial, Helvetica, sans-serif">Asthma</td>
<td class="auto-style31">
<asp:CheckBox ID="GlaucomaCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Glaucoma</td>
<td class="auto-style39">
<asp:CheckBox ID="KidneyCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Kidney Problems</td>
<td class="auto-style76">
<asp:CheckBox ID="SinusCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Sinus Trouble</td>
<td class="auto-style76">
</td>
</tr>
<tr><td class="auto-style85">
<asp:CheckBox ID="CancerCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Cancer</td>
<td class="auto-style39">
<asp:CheckBox ID="HeartAtkCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Heart Attack</td>
<td class="auto-style39">
<asp:CheckBox ID="LeukemiaCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Leukemia</td>
<td class="auto-style76">
<asp:CheckBox ID="SpinaBifidaCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Spina Bifida</td>
<td class="auto-style76">
</td>
</tr>
<tr><td class="auto-style87">
<asp:CheckBox ID="ColdSoreCbox" runat="server" Text=" " />
</td>
<td class="auto-style36" style="font-family: Arial, Helvetica, sans-serif">Cold Sores/Fever Blisters</td>
<td class="auto-style33">
<asp:CheckBox ID="HeartMurmurCbox" runat="server" Text=" " />
</td>
<td class="auto-style62" style="font-family: arial, Helvetica, sans-serif">Heart Murmur</td>
<td class="auto-style65">
<asp:CheckBox ID="LiverCbox" runat="server" Text=" " />
</td>
<td class="auto-style30" style="font-family: arial, Helvetica, sans-serif">Liver Disease</td>
<td class="auto-style74">
<asp:CheckBox ID="StomachCbox" runat="server" Text=" " />
</td>
<td class="auto-style64" style="font-family: arial, Helvetica, sans-serif">Stomach/Intestinal Disease</td>
<td class="auto-style77">
</td>
</tr>
<tr><td class="auto-style88">
<asp:CheckBox ID="HeartDisorderCbox" runat="server" Text=" " />
</td>
<td class="auto-style122" style="font-family: Arial, Helvetica, sans-serif">Congenital Heart Disorder</td>
<td class="auto-style90">
<asp:CheckBox ID="HeartDiseaseCbox" runat="server" Text=" " />
</td>
<td class="auto-style122" style="font-family: arial, Helvetica, sans-serif">Heart Disease</td>
<td class="auto-style90">
<asp:CheckBox ID="JawPainCbox" runat="server" Text=" " />
</td>
<td class="auto-style124" style="font-family: arial, Helvetica, sans-serif">Pain in Jaw Joints</td>
<td class="auto-style92">
<asp:CheckBox ID="StrokeCbox" runat="server" Text=" " />
</td>
<td class="auto-style124" style="font-family: arial, Helvetica, sans-serif">Stroke</td>
<td class="auto-style92">
</td>
</tr>
<tr><td class="auto-style85">
<asp:CheckBox ID="DiabetesCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Diabetes</td>
<td class="auto-style39">
<asp:CheckBox ID="HepACbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hepatitis A</td>
<td class="auto-style39">
<asp:CheckBox ID="ParathyroidCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Parathyroid Disease</td>
<td class="auto-style76">
<asp:CheckBox ID="ThyroidCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Thyroid Disease</td>
<td class="auto-style76">
</td>
</tr>
<tr><td class="auto-style85">
<asp:CheckBox ID="DigestiveCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Digestive Disorders</td>
<td class="auto-style39">
<asp:CheckBox ID="HepBorCCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">Hepatitis B or C</td>
<td class="auto-style39">
<asp:CheckBox ID="PsychCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Psychiatric Care</td>
<td class="auto-style76">
<asp:CheckBox ID="TumorCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Tumors or Growths</td>
<td class="auto-style76">
</td>
</tr>
<tr><td class="auto-style85">
<asp:CheckBox ID="DrugCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: Arial, Helvetica, sans-serif">Drug Addiction</td>
<td class="auto-style39">
<asp:CheckBox ID="HBPCbox" runat="server" Text=" " />
</td>
<td class="auto-style121" style="font-family: arial, Helvetica, sans-serif">High Blood Pressure</td>
<td class="auto-style39">
<asp:CheckBox ID="RheumatismCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Rheumatism</td>
<td class="auto-style76">
<asp:CheckBox ID="UlcerCbox" runat="server" Text=" " />
</td>
<td class="auto-style123" style="font-family: arial, Helvetica, sans-serif">Ulcers</td>
<td class="auto-style76">
</td>
</tr>
</table>
</fieldset>
什麼是從數據表映射到複選框?另外我建議使用SqlCommand.Paramaters進行硬編碼查詢。 –
你有沒有想過創建一個可以透視數據的存儲過程? – JamieD77
短期解決方案,更改複選框的ID屬性,以將AilmentID包含在「AilmentCbox_1」,「AilmentCbox_2」,「AilmentCbox_3」等可預測的模式中,以便您可以將控件與數字值進行匹配。 長期的解決方案,將row_number和column_number列添加到您的疾病表中,以便動態生成數據庫中的複選框控件,而不是在您的ASPX頁面中進行硬編碼。 – user2638401