【问题标题】:INSERT more than one row in table on button click单击按钮时在表格中插入多行
【发布时间】:2015-08-05 03:30:59
【问题描述】:

此代码工作正常,但其 INSERTING 值仅来自 textbox1 textbox2 和 Date2 如果我有更多文本框和标签如何一键从不同控件插入多行。

确切地说,我有 10 对文本框文本框 1 和文本框 2,然后是文本框 3 和文本框 4 和 10 个标签 日期 2 / 4 /6 / 8

所以在每一行我想要来自 textbox[i] textbox[i+1] Date[i+1] 和全局变量 buyEUR 的值

private void InsertData_Click(object sender, EventArgs e)
{

    SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\Users\FluksikartoN\Documents\Visual Studio 2012\Projects\BuroFoki\BuroFoki\MainDB.sdf");
    connection.Open();

    using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR, @Rate, @BGN, @Date)", connection))
    {
       /* for (int i = 2; i <= 20; i = i+2)
       {
          TextBox txtBox1 = this.Controls["TextBox" + (i - 1).ToString()] as TextBox;
          TextBox txtBox2 = this.Controls["TextBox" + i.ToString()] as TextBox;
          Label Date = this.Controls["Date" + i.ToString()] as Label;*/
          com.Parameters.AddWithValue("@EUR", textBox2.Text.ToString());
          com.Parameters.AddWithValue("@Rate", EURbuy);
          com.Parameters.AddWithValue("@BGN", textBox1.Text.ToString());
          com.Parameters.AddWithValue("@Date", Date2.Text.ToString());
          /*
          com.Parameters.AddWithValue("@EUR", textBox4.Text.ToString());
          com.Parameters.AddWithValue("@Rate", EURbuy);
          com.Parameters.AddWithValue("@BGN", textBox3.Text.ToString());
          com.Parameters.AddWithValue("@Date", Date4.Text.ToString());
          */
          com.ExecuteNonQuery();

       }

       connection.Close();

     }

【问题讨论】:

  • 我不确定这一点,但也许你应该只添加一次参数,在循环之前 (com.Parameters.Add("@EUR", [ParamType])),然后,在循环内,只需设置参数的值 (com.Parameters["@EUR"].Value = textBox4.Text.ToString()) 并执行查询。
  • 你能举个例子吗,我不确定 ExecuteNonQuery 是否必须在循环内,请给我举个例子来更好地理解这个想法
  • 为什么不能使用DataGridViewDataGrid 控件。使用 htat 控件,您可以摆脱多个文本框和标签
  • 已经开始使用文本框和标签。为它们设置样式比 DataGrid/View 更好。对 DataGrid 没有太多经验,但我尝试过但无法删除一些我不想在网格中删除的东西

标签: c# sql .net sql-server-ce


【解决方案1】:

您可以创建一个控制数组See here(尽管没有固有的支持,您可以复制该功能)然后您可以使用循环获取要添加为参数的值并将值插入数据库中。

【讨论】:

  • 但是在我创建了所有需要的控件之后,即使我删除了围绕com.Parameters.AddWithValue/**/,我如何在我的示例中应用多个行,它给我一个错误
  • 您的 Insert 语句有 4 个参数,如果您删除 remove /**/ 那么您将多次添加相同的参数,因此您会收到错误消息。您需要将插入语句保留在循环内,然后在循环内添加参数(来自控制数组)。
  • 所以loop 必须在connection.open() 之后开始并在connection.close() 之前结束
  • 是的。关闭连接后不能插入值。
【解决方案2】:

试试这是否可行。

using (SqlCeCommand com = new SqlCeCommand("INSERT INTO TenOperations (EUR, Rate, BGN, Date) Values(@EUR1, @Rate1, @BGN1, @Date1),(@EUR2, @Rate2, @BGN2, @Date2)", connection))
     {
        com.Parameters.AddWithValue("@EUR1", textBox2.Text.ToString());
        com.Parameters.AddWithValue("@Rate1", EURbuy);
        com.Parameters.AddWithValue("@BGN1", textBox1.Text.ToString());
        com.Parameters.AddWithValue("@Date1", Date2.Text.ToString());

        com.Parameters.AddWithValue("@EUR2", textBox4.Text.ToString());
        com.Parameters.AddWithValue("@Rate2", EURbuy);
        com.Parameters.AddWithValue("@BGN2", textBox3.Text.ToString());
        com.Parameters.AddWithValue("@Date2", Date4.Text.ToString());

        com.ExecuteNonQuery();
     }

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2012-02-24
    • 2017-09-21
    相关资源
    最近更新 更多