Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重复输出目标-OleDB_C# - Fatal编程技术网

C# 重复输出目标-OleDB

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&

我尝试在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> 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()函数

通过清除参数集合,您正在为下一个查询做准备

这有用吗

编辑: 至少可以说,你的方法有点不正统。但它可以工作。以下是一些给你的建议:

  • 手动构造DBCommand命令文本和所需参数,以便从textBoxChild1、textBox1Birthday、textBoxChild1EMail和所需的任何其他字段插入数据。一旦你开始工作,用textBoxChild2再做一次,看看有什么变化。构造循环,以便每次迭代都根据需要调整插入查询和参数

  • 考虑不使用textBoxChild1和childName字典映射。相反,创建一个子对象,然后创建一个子对象的通用列表(即list)。在该通用列表中,每个列表元素都是一个子对象。然后,您可以通过迭代列表来构造数据库操作。有很多例子说明了如何在C#中遍历泛型列表对象

  • 如果前端用户界面上的文本框包含多行信息,请改用网格。您不应该有textBoxChild1 UI元素和textBoxChild2元素——这是一个需要管理的噩梦,特别是当您需要添加新的Child7或Child8,或者如果您要为每个孩子添加更多字段时。您需要找到适合集合和对象的设计,并相应地构造插入查询

  • 不要对自己太苛刻。你最初的设计是非常有缺陷的,我不建议仅仅因为你投入了时间就保留它。为了可读性和可维护性,应更改其设计


    希望这有帮助。

    您没有提供足够的信息来提供明确的答案。但您需要的是在每个子级之后插入记录到