2016-03-15 85 views
0

我在网上看,在大多数示例中TableAdapter.Update工作得很好。我不知道我的代码中出了什么问题,但它不适用于我(数据在DataSet或Access DataBase中没有更改)。 希望你能帮我解决这个问题。TableAdapter.Update不保存数据中的更改

(对不起,我英文不好)

namespace myDB 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void tableSelection_Click(object sender, EventArgs e) 
    { 

     switch (tableSelection.SelectedIndex) 
     { 
      case 0: mainTable.DataSource = компанияBindingSource; 
       myBindingNavigator.BindingSource = компанияBindingSource; 

       break; 
      case 1: mainTable.DataSource = обслуживающийПерсоналBindingSource; 
       myBindingNavigator.BindingSource = обслуживающийПерсоналBindingSource; 

       break; 
      case 2: mainTable.DataSource = персоналBindingSource; 
       myBindingNavigator.BindingSource = персоналBindingSource; 

       break; 
      case 3: mainTable.DataSource = пользовательBindingSource; 
       myBindingNavigator.BindingSource = пользовательBindingSource; 

       break; 
      case 4: mainTable.DataSource = продуктBindingSource; 
       myBindingNavigator.BindingSource = продуктBindingSource; 

       break; 
      case 5: mainTable.DataSource = проектыBindingSource; 
       myBindingNavigator.BindingSource = проектыBindingSource; 

       break; 
      case 6: mainTable.DataSource = разработчикиBindingSource; 
       myBindingNavigator.BindingSource = разработчикиBindingSource; 

       break; 

      default: MessageBox.Show("Проверьте корректность выбранного значения!"); 
       break; 
     } 

} 



    public void Form1_Load(object sender, EventArgs e) 
    { 
     // TODO: This line of code loads data into the 'bDDataSet.Разработчики' table. You can move, or remove it, as needed. 
     this.разработчикиTableAdapter.Fill(this.bDDataSet.Разработчики); 
     // TODO: This line of code loads data into the 'bDDataSet.Проекты' table. You can move, or remove it, as needed. 
     this.проектыTableAdapter.Fill(this.bDDataSet.Проекты); 
     // TODO: This line of code loads data into the 'bDDataSet.Продукт' table. You can move, or remove it, as needed. 
     this.продуктTableAdapter.Fill(this.bDDataSet.Продукт); 
     // TODO: This line of code loads data into the 'bDDataSet.Пользователь' table. You can move, or remove it, as needed. 
     this.пользовательTableAdapter.Fill(this.bDDataSet.Пользователь); 
     // TODO: This line of code loads data into the 'bDDataSet.Персонал' table. You can move, or remove it, as needed. 
     this.персоналTableAdapter.Fill(this.bDDataSet.Персонал); 
     // TODO: This line of code loads data into the 'bDDataSet.обслуживающий_персонал' table. You can move, or remove it, as needed. 
     this.обслуживающий_персоналTableAdapter.Fill(this.bDDataSet.обслуживающий_персонал); 
     // TODO: This line of code loads data into the 'bDDataSet.Компания' table. You can move, or remove it, as needed. 
     this.компанияTableAdapter.Fill(this.bDDataSet.Компания); 



    } 

    public void updateButton_Click(object sender, EventArgs e) 
    { 

     пользовательBindingSource.EndEdit(); 
     пользовательTableAdapter.Update(bDDataSet.Пользователь); 
     bDDataSet.AcceptChanges(); 




    } 







} 
} 

Screenshot of program

回答

0
try 
{ 
     Validate(); 
     abcFormBindingSource.EndEdit(); 
     abcViewFormTableAdapter.UpdateQuery(YourParameter) 
     MessageBox.Show("Update successful"); 
} 


catch (System.Exception ex) 
{ 
     MessageBox.Show("Update failed " + ex.ToString()); 
     return; 
} 
+0

谢谢,但我没办法UpdateQuery在我的TableAdapter –

+0

转到您的数据源>编辑数据集与设计权点击你的表格>添加查询>下一步>选择更新,然后插入你的更新代码按下一步而你必须在我的情况下选择你的方法名称UpdateQuery然后芬兰 –

+0

我找到解决方案,也许它会帮助有这样的问题的人: 数据库有两个副本:应用程序文件夹中有1个,bin \ debug文件夹中有1个。在设计时,应用程序文件夹中的数据库在运行时使用另一个。 默认情况下,在解决方案资源管理器中,数据库设置为始终复制到bin \ debug文件夹,这意味着数据库被应用程序文件夹中的一个覆盖。 解决方案:我将此属性更改为“COPY IF NEWER”,现在它可以工作。 –