问题描述
我在更新数据集/数据库时遇到问题.
有一个名称为"Age"的表"TblPerson".该表单包括一个"TblPerson"的数据网格视图,一个"IDPerson"的组合框,一个"Age"的文本框以及用于加载数据库,更新数据集并保存到数据库的按钮.
将数据加载到datagridview和组合框可以正常工作.当我在年龄"文本字段中进行更改并按更新"按钮时,datagridview将显示更改.
但是,当我希望将其保存到数据库中时,没有注册要更新的数据.
但是在datagridview中执行了direkt的更改之后,将其保存到数据库即可正常工作.
我对此感到奇怪,因为数据集始终显示正确的更新数据.也许组合框和文本字段无法在数据集中显示更改.
有人可以帮我吗?
谢谢,最好的问候!
Hi,
I have a problem updating a dataset / database.
There is a table "TblPerson" with the name and the "Age". The form consids a datagridview of "TblPerson", a combobox for "IDPerson", a textbox for "Age" and buttons for loading the database, updating the dataset and saving to the Database.
Loading the data to the datagridview and the comboboxes works fine. When I do changes in the "Age" textfield and press the update button, the datagridview shows the changes.
But when I want so save this to the database, there is no data registered to be updated.
But after doing the changes direkt in the datagridview, saving to the DB works fine.
I wonder about this, because the dataset always shows the correct updated data. Maybe the comboboxes and textfields do not registrate the change in the dataset.
Can somebody help me?
Thanks and best regards!
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Test3_Join
{
public partial class Form1 : Form
{
private DataSet ds = new DataSet();
private DataViewManager dsView;
OleDbDataAdapter AdapterTblPerson;
OleDbConnection con;
OleDbCommand cmd = new OleDbCommand();
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
string connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\...\\Database.accdb;Persist Security Info=False;";
con = new OleDbConnection(connetionString);
ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
cmd.Connection = con;
cmd.CommandText = "SELECT * FROM TblPerson";
AdapterTblPerson = new OleDbDataAdapter(cmd);
AdapterTblPerson.Fill(ds, "TblPerson");
dsView = ds.DefaultViewManager;
cboIDConnector.DataSource = dsView;
cboIDConnector.DisplayMember = "TblPerson.IDPerson";
cboIDConnector.ValueMember = "TblPerson.IDPerson";
tbPinAmount.DataBindings.Add("Text", dsView, "TblPerson.Age");
}
private void btnUpdate_Click(object sender, EventArgs e)
{
OleDbCommandBuilder cb = new OleDbCommandBuilder(AdapterTblPerson);
int count = AdapterTblPerson.Update(ds.Tables["TblPerson"]);
textBox1.Text = count.ToString();
textBox1.AppendText("\r" + cb.GetUpdateCommand().ToString());
}
private void btnLoadTable_Click(object sender, EventArgs e)
{
dataGridView1.Refresh();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "TblPerson";
}
}
}
推荐答案
这篇关于将数据集更新为数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!