我目前正在尝试在多个文本框中创建所有非空值以与插入查询中的参数匹配。我面临的问题是我正在使用的数据库表中的每个字段重复输入相同的值。我已经在谷歌和堆栈溢出调查了这一点,虽然显然推荐for循环foreach,我找不到我的问题的具体答案。
这是我的代码:
List<KeyValuePair<string, string>> listOfTextboxes = new List<KeyValuePair<string, string>>();
private void InsertData()
{
using (dbConn)
{
dbConn.Open();
using (var dbCmd = new OleDbCommand("INSERT INTO members (household_head, birthday, phone, email, address, status, spouse, spouse_phone, spouse_email, " +
"anniversary, spouse_status, child1, child1_birthday, child1_email, " +
"child2, child2_birthday, child2_email, child3, child3_birthday, child3_email, child4, child4_birthday, child4_email, child5, child5_birthday, child5_email," +
"child6, child6_birthday, child6_email, child7, child7_birthday, child7_email) " +
"VALUES (@household_head, @birthday, @phone, @email, @address, @status, @spouse, @spouse_phone, @spouse_email, @anniversary, @spouse_status," +
"@child1, @child1_birthday, @child1_email, " +
"@child2, @child2_birthday, @child2_email, @child3, @child3_birthday, @child3_email, @child4, @child4_birthday, @child4_email," +
"@child5, @child5_birthday, @child5_email, @child6, @child6_birthday, @child6_email, @child7, @child7_birthday, @child7_email)", dbConn))
{
try
{
InsertDBParameters(dbCmd);
dbCmd.ExecuteNonQuery();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
MessageBox.Show("Record inserted.");
}
}
private void InsertDBParameters(OleDbCommand cmd)
{
List<String> checkBoxes = new List<String>();
foreach (Control cbox in Controls)
{
if (cbox is CheckBox && ((CheckBox)cbox).Checked)
{
checkBoxes.Add("Member");
}
else if (cbox is CheckBox && !((CheckBox)cbox).Checked)
{
checkBoxes.Add("Regular Attender");
}
}
foreach (Control c in Controls)
{
if (c is TextBox && !string.IsNullOrEmpty(((TextBox)c).Text))
{
listOfTextboxes.Add(new KeyValuePair<string, string>(((TextBox)c).Name, ((TextBox)c).Text));
}
}
for (int i = 0; i < listOfTextboxes.Count; i++)
{
// loop through the list and assign each textbox field's not empty value
// to the corresponding field in the database table members
if (i == listOfTextboxes.Count - 1)
{
// last item
}
cmd.Parameters.AddWithValue("@household_head", listOfTextboxes[i]);
/*
cmd.Parameters.AddWithValue("@household_head", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@phone", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@address", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@status", checkBoxes[0]);
cmd.Parameters.AddWithValue("@spouse", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@spouse_phone", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@spouse_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@anniversary", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@spouse_status", checkBoxes[0]);
cmd.Parameters.AddWithValue("@child1", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child1_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child1_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child2", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child2_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child2_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child3", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child3_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child3_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child4_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child5_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child6", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child6_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child6_email", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child7", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child7_birthday", listOfTextboxes[i]);
cmd.Parameters.AddWithValue("@child7_email", listOfTextboxes[i]);
*/
}
}
并调用insert事件本身:
insertRecordsButton.Click += (sender, args) =>
{
InsertData();
};
我想在InsertDBParameters
方法中使用确切的参数,但我不确定如何避免为每列插入相同的值。我很抱歉,如果这是一个愚蠢的问题,但我已经坚持了一天,并且不知道如何处理这个问题。
任何帮助都将不胜感激。
谢谢!
答案 0 :(得分:1)
您可以将SQL命令中的参数名称更改为文本框的名称(如果它们与参数名称不同):
Float.parse
并更改此行:
VALUES (@txt_household_head, @txt_birthday, @txt_phone, @txt_email, ...
到
cmd.Parameters.AddWithValue("@household_head", listOfTextboxes[i]);
我会考虑使用cmd.Parameters.AddWithValue(String.Format("@{0}", listOfTextboxes[i].Key.ToString()), listOfTextboxes[i].Value);
代替Dictionary