C# 重复输出目标-OleDB
我尝试在foreach循环下执行一个insert查询,以插入多个值。但是,当我添加多个文本框行时,会出现一个错误,显示为C# 重复输出目标-OleDB,c#,C#,我尝试在foreach循环下执行一个insert查询,以插入多个值。但是,当我添加多个文本框行时,会出现一个错误,显示为System.Data.OleDb.OleDbException:Duplicate output destination at'childName' 这是我的密码: // use LINQ to fetch all the children textboxes System.Collections.Generic.Dictionary<string, string&
System.Data.OleDb.OleDbException:Duplicate output destination at'childName'
这是我的密码:
// use LINQ to fetch all the children textboxes
System.Collections.Generic.Dictionary<string, string> dictionary = tabCtrl1.TabPages["tabPage1"].Controls.OfType<TextBox>()
.Where(t => t.TabIndex >= startTabIndex && t.TabIndex <= endTabIndex && !string.IsNullOrWhiteSpace(t.Text))
.OrderBy(f => f.TabIndex)
.Select(x => new System.Collections.Generic.KeyValuePair<string, string>(x.Name, x.Text))
.ToDictionary(z => z.Key, z => z.Value);
// loop through all the children textboxes
// and assign them to the list members.childTextBoxes
foreach (System.Collections.Generic.KeyValuePair<string, string> kvp in dictionary)
{
members.childTextBoxes.Add(new System.Collections.Generic.KeyValuePair<string, string>($"{kvp.Key}", $"{kvp.Value}"));
}
var mapToDatabase = new System.Collections.Generic.Dictionary<string, string>();
mapToDatabase.Add(nameof(textBoxChild1), "childName");
mapToDatabase.Add(nameof(textBoxChild1Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild1Email), "childEmail");
// child 2
mapToDatabase.Add(nameof(textBoxChild2), "childName");
mapToDatabase.Add(nameof(textBoxChild2Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild2Email), "childEmail");
// child 3
mapToDatabase.Add(nameof(textBoxChild3), "childName");
mapToDatabase.Add(nameof(textBoxChild3Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild3Email), "childEmail");
// child 4
mapToDatabase.Add(nameof(textBoxChild4), "childName");
mapToDatabase.Add(nameof(textBoxChild4Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild4Email), "childEmail");
// child 5
mapToDatabase.Add(nameof(textBoxChild5), "childName");
mapToDatabase.Add(nameof(textBoxChild5Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild5Email), "childEmail");
// child 6
mapToDatabase.Add(nameof(textBoxChild6), "childName");
mapToDatabase.Add(nameof(textBoxChild6Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild6Email), "childEmail");
// child 7
mapToDatabase.Add(nameof(textBoxChild7), "childName");
mapToDatabase.Add(nameof(textBoxChild7Birthday), "birthday");
mapToDatabase.Add(nameof(textBoxChild7Email), "childEmail");
string fieldList = $"{string.Join(",", members.childTextBoxes.Select(tb => mapToDatabase[tb.Key]))}";
string valueList = $"{string.Join(",", members.childTextBoxes.Select(tb => "?"))}";
string insertQuery = $"INSERT INTO children (pid, {fieldList}) VALUES (?,{valueList})";
using (members.DBCommand = new OleDbCommand(insertQuery, members.DBConnection))
{
foreach (var field in members.childTextBoxes)
{
members.DBCommand.Parameters.AddWithValue("@pid", lastInsertId);
members.DBCommand.Parameters.Add("@" + field.Key, OleDbType.LongVarChar).Value = field.Value;
if (members.DBCommand.ExecuteNonQuery() > 0) // error line
{
members.DBCommand.Parameters.Clear();
}
}
MessageBox.Show("Records inserted", "QBC", MessageBoxButtons.OK);
// clear the fields
foreach (Control c in Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
}
}
//使用LINQ获取所有子文本框
System.Collections.Generic.Dictionary Dictionary=tabCtrl1.TabPages[“tabPage1”].Controls.OfType()
.Where(t=>t.TabIndex>=startTabIndex&&t.TabIndex f.TabIndex)
.Select(x=>newsystem.Collections.Generic.KeyValuePair(x.Name,x.Text))
.ToDictionary(z=>z.Key,z=>z.Value);
//循环浏览所有子文本框
//并将它们分配给列表成员。ChildTextBox
foreach(字典中的System.Collections.Generic.KeyValuePair kvp)
{
members.childtextboxs.Add(new System.Collections.Generic.KeyValuePair($“{kvp.Key},$”{kvp.Value}”);
}
var mapToDatabase=new System.Collections.Generic.Dictionary();
Add(nameof(textBoxChild1),“childName”);
添加(姓名(textBoxChild1Birthday),“生日”);
Add(name of(textBoxChild1Email),“childEmail”);
//儿童2
Add(nameof(textBoxChild2),“childName”);
添加(姓名(textBoxChild2Birthday),“生日”);
Add(nameof(textBoxChild2Email),“childEmail”);
//儿童3
Add(nameof(textBoxChild3),“childName”);
添加(姓名(textboxchild3birth),“生日”);
Add(name of(textBoxChild3Email),“childEmail”);
//儿童4
Add(nameof(textBoxChild4),“childName”);
添加(name of(textboxchild4birth),“birth”);
Add(name of(textBoxChild4Email),“childEmail”);
//儿童5
Add(nameof(textBoxChild5),“childName”);
添加(姓名(textBoxChild5Birthday),“生日”);
Add(name of(textBoxChild5Email),“childEmail”);
//儿童6
Add(nameof(textBoxChild6),“childName”);
添加(姓名(textBoxChild6Birthday),“生日”);
Add(name of(textBoxChild6Email),“childEmail”);
//儿童7
Add(nameof(textBoxChild7),“childName”);
添加(姓名(textBoxChild7Birthday),“生日”);
Add(name of(textBoxChild7Email),“childEmail”);
字符串字段列表=$“{string.Join(“,”,members.childTextBox.Select(tb=>mapToDatabase[tb.Key]))}”;
字符串值列表=$“{string.Join(“,”,members.childTextBox.Select(tb=>“?”)}”;
string insertQuery=$“插入子(pid,{fieldList})值(?,{valueList})”;
使用(members.DBCommand=new-OleDbCommand(insertQuery,members.DBConnection))
{
foreach(members.childTextBox中的变量字段)
{
members.DBCommand.Parameters.AddWithValue(“@pid”,lastInsertId);
members.DBCommand.Parameters.Add(“@”+field.Key,OleDbType.LongVarChar).Value=field.Value;
if(members.DBCommand.ExecuteOnQuery()>0)//错误行
{
members.DBCommand.Parameters.Clear();
}
}
显示(“插入的记录”,“QBC”,MessageBox按钮.OK);
//清理田地
foreach(控件中的控件c)
{
如果(c是文本框)
{
((文本框)c).清除();
}
}
}
我认为这与map.ToDatabase.Add()部分有关。有没有办法避免这种情况,并且仍然对填写的每个文本框字段执行insert查询?其他一切正常,我只是不能添加多个文本框行
任何帮助都将不胜感激
谢谢 运行members.DBCommand.ExecuteNonQuery()后,需要在某处添加members.DBCommand.Parameters.Clear()。您还应该在处理成员.childTextBox字典项的for循环中调用成员.DBCommand.ExecuteOnQuery()函数 通过清除参数集合,您正在为下一个查询做准备 这有用吗 编辑: 至少可以说,你的方法有点不正统。但它可以工作。以下是一些给你的建议:
希望这有帮助。您没有提供足够的信息来提供明确的答案。但您需要的是在每个子级之后插入记录到