【问题标题】:saving data at the Database SQLSERVER在数据库 SQL SERVER 中保存数据
【发布时间】:2021-05-14 04:21:21
【问题描述】:

我想在数据库中保存数据时遇到问题。 例外是:

System.InvalidOperationException: 'DataColumn' Name_Faculty 'missing in DataTable' disconnGrid 'for SourceColumn' Name_Faculty '。'

public void fillGrid()
        {
            if (adoF2.ds.Tables["disconnGrid"] != null)
            {
                adoF2.ds.Tables["disconnGrid"].Clear(); // to get new update.
            }
                adoF2.da = new SqlDataAdapter("SELECT * FROM Students", adoF2.con1);
                adoF2.da.Fill(adoF2.ds, "disconnGrid");
                dataGridView1.DataSource = adoF2.ds.Tables["disconnGrid"];
        }
public void fillCombo()
        {
            adoF2.da = new SqlDataAdapter("SELECT * FROM Faculty", adoF2.con1);
            adoF2.da.Fill(adoF2.ds, "disconnCombo");
            comboBox1.DataSource = adoF2.ds.Tables["disconnCombo"];
            comboBox1.DisplayMember = adoF2.ds.Tables["disconnCombo"].Columns[1].ColumnName;
            comboBox1.ValueMember = adoF2.ds.Tables["disconnCombo"].Columns[0].ColumnName; /* this column is a foreign key into 'Students' table*/
        }
private void gestionStagiaire2_Load(object sender, EventArgs e)
        {
            fillGrid();
            fillCombo();
        }
// to add the new row in the dataTable[disconnGrid]
private void button1_Click(object sender, EventArgs e)
        {
            adoF2.row1 = adoF2.ds.Tables["disconnGrid"].NewRow();
            adoF2.row1[0] = textID.Text;
            adoF2.row1[1] = textName.Text;
            adoF2.row1[2] = comboBox1.SelectedValue; // to get the value not what is diplayed on the UI of comboBox1.
            for (int i = 0; i < adoF2.ds.Tables["disconnGrid"].Rows.Count; i++)
            {
                if (textID.Text == adoF2.ds.Tables["disconnGrid"].Rows[i].ToString())
                {
                    MessageBox.Show("Sorry the students is existed", "Warning", MessageBoxButtons.OK);
                    return; // to avoid errors.
                }
            }
            adoF2.ds.Tables["disconnGrid"].Rows.Add(adoF2.row1);
            MessageBox.Show("The students has been added successfully", "info", MessageBoxButtons.OK,MessageBoxIcon.Information);
            dataGridView1.DataSource = adoF2.ds.Tables["disconnGrid"]; // to get the newly update.
        }
// to save the data into the database:
private void saveB_Click(object sender, EventArgs e)
        {
            adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);
            adoF2.da.Update(adoF2.ds.Tables["disconnGrid"]);
            MessageBox.Show("Saving has been succeeded", "Info", MessageBoxButtons.OK);
        }

【问题讨论】:

  • 这里没有太多信息。为什么会要求Name_Faculty 列,它在哪里声明?旁白:I hope you are closing and disposing your connection
  • Name_Faculty 是表中的一列Faculty
  • 显然这两个表混淆了。而且我想它引出了一个问题,为什么要缓存适配器并重用它,至少每个表都有一个不同的适配器。这行adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da); 似乎没有做任何事情,不知道为什么它在那里,看起来你可能想改变适配器的 Command 属性
  • adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da); 用于自动生成 INSERT 查询以使adoF2.da SqlDataAdapter 对象向数据库中添加新行。
  • 我只是没有看到代码。我看不到adoF2 的声明,所以不知道它是否有任何代码可以做到这一点,但我猜它没有,那是你的问题。

标签: sql-server datatable combobox dataset ado.net


【解决方案1】:

试试这个代码:

            adoF2.cmdbuilder = new SqlCommandBuilder(adoF2.da);
            adoF2.da.Update(adoF2.ds,"disconnGrid");
            MessageBox.Show("Saving has been succeeded", "Info", MessageBoxButtons.OK);

【讨论】:

  • 是的,我用过。错误是将两个代码排列到 form_Load 事件中,如果我颠倒排列,程序将毫无问题地运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
相关资源
最近更新 更多