本文介绍了如何将DataTable ID与DB同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发一个简单的DB应用程序.我是一个完全的C#初学者,所以请对我轻松一点:)在我的应用程序中,我有一个DB,其表包含3列(ID +两个数据字段),还有一个带有显示此内容的ListBox的简单表单 表,两个允许输入要存储在DB中的数据的文本框和两个按钮:新建"和删除". ListBox DataSource设置为在简单的SELECT查询上创建的DataTable对象.我正在使用MySQL数据库,这可能是与MySQL连接器有关的问题, 但是我可能只是想念一些东西.

I am developing a simple DB application in C#. I am a complete C# beginner, so please be easy on me :) In my application I have a DB with table containing 3 columns (ID + two data fields), and also a simple form with a ListBox displaying contents of this table, two TextBoxes allowing to enter data to be stored in DB, and two Buttons: Create New and Delete. ListBox DataSource is set to a DataTable object created on simple SELECT query. I am using a MySQL database and this could be problem related to MySQL connector, but also I might be simply missing something.

现在,一切对我来说都很好,我能够创建这样的新记录:

Now, everything was fine for me and I was able to create a new record like this:

// this is the DataTable used as ListBox data source DataTable t = tag_.Table; DataRow newRow = t.NewRow(); newRow.BeginEdit(); newRow["name"] = txtTagName.Text.Trim(); newRow["description"] = txtDescriptionName.Text.Trim(); newRow.EndEdit(); t.Rows.Add(newRow); try {

//这将调用daTag_.Update(dsTag_,TABLE),其中daTag_是MySqlDataAdapter,dsTag_是DataSet,而TABLE是表名: tag_.Update();}抓(MySqlException ex){ MySQLExceptionHandler.Show(ex); tag_.RejectChanges();}

// this calls daTag_.Update(dsTag_, TABLE), where daTag_ is MySqlDataAdapter, dsTag_ is the DataSet and TABLE is the table name:) tag_.Update();}catch (MySqlException ex){ MySQLExceptionHandler.Show(ex); tag_.RejectChanges();}

在我发现我需要使用记录的ID之前(这无关紧要),这一直很好.因此,我还必须从数据库中选择ID.这就是我的问题开始的地方.创建新的DataRow时,我将NULL放入ID字段,以便获取 来自AUTO_INCREMENT的新值.成功调用Update()之后,数据将以自动生成的ID值存储在数据库中,但是添加了行的DataTable对象仍然保留已成功插入到数据库中的行ID的NULL值. 所以:

This was working fine until I found that I need to use the records' ID (doesn't matter for what). So I had to select also the ID from DB. And this is where my problems started. When creating a new DataRow, I am putting NULL into ID field, so that it gets new value from AUTO_INCREMENT. After successful call to Update(), the data gets stored in the DB with auto-generated ID value, BUT the DataTable object, to which the row was added, still holds the NULL value for the ID of Row successfuly inserted into DB. So:

  • 在我的应用程序中,我有一个DataTable,其中DataRow的ID字段的ID字段中包含NULL
  • 在我的数据库中,我有一条具有自动生成的ID值的新记录.

因此,DataTable和DB之间的数据不同步.现在您可以想象如果我尝试删除刚插入的行将会发生什么. NULL将以某种方式转换为0并...

So the data is not synchronized between the DataTable and DB. Now you can imagine what will happen if I try to DELETE the row I just inserted. The NULL will get somehow translated to 0 and...

我为此提出了以下解决方案:

I came up with following solution for this:

try {

    // this calls daTag_.Update(dsTag_, TABLE), where daTag_ is MySqlDataAdapter, dsTag_ is the DataSet and TABLE is the table name:)
    tag_.Update();
    newRow["id"] = tag_.LastInsertedId;
    newRow.AcceptChanges();
}
catch (MySqlException ex)
{
    MySQLExceptionHandler.Show(ex);
    tag_.RejectChanges();
}

这项工作正常,但我必须记住每次插入时的这一额外更改,在我看来,这很像黑客.有没有更好的解决方案来使DataTable在插入时同步?


This works OK, but I have to remember about this additional change on every insert, and it seems to me quite like a hack. Is there any better solution to get DataTable synchronized on insert?


推荐答案

 欢迎使用MSDN论坛支持.

  Welcome to MSDN Forum Support.

 根据您的描述,是否将DB表的属性配置为索引ID内的AUTO_INCREMENT令我感到困惑.如果这样做,我觉得您不必担心会将NULL值插入 种子末将帮助您自动处理这种情况.

  Based on your descriptions, I am confused by the situation whether you configure your the property of your DB table into AUTO_INCREMENT within its index id. If you have done that, I feel you are not worry about the NULL value would be inserted into the end of table.Seed will help you automatically deal with this situation.

 真诚的

 贾森·王(Jason Wang)

  Jason Wang


这篇关于如何将DataTable ID与DB同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:25
查看更多