2013-04-10 48 views
0

美好的一天!TableAdapter不保存对SQL的更改表

我是C#的新手。

我有数据集在Windows窗体中,我已经添加了一个记录数据集。然后我调用TableAdapter.Update(MyTable)(RowState处于增加模式)。进行了更改,我可以在绑定到SQL MyTable的DataGridView中看到它们。我可以关闭我的应用程序并重新开始,我会看到添加的记录。

private void button1_Click(object sender, EventArgs e) 
    { 
     DimaeSQLDS dsAddToDima = new DimaeSQLDS(); 

     using (DimaeSQLDSTableAdapters.OrganizationsTableAdapter orgAdapter = new DimaeSQLDSTableAdapters.OrganizationsTableAdapter()) 
     { 
      orgAdapter.Fill(dsAddToDima.Organizations); 
      DimaeSQLDS.OrganizationsRow organizationsRow = dsAddToDima.Organizations.NewOrganizationsRow(); 
      organizationsRow.Address = tbINN.Text; 
      organizationsRow.OrgName = tbOrgName.Text; 
      organizationsRow.UrFiz = 0; 
      dsAddToDima.Organizations.Rows.Add(organizationsRow); //adds row to DataSet  
      this.Validate(); 
      orgAdapter.Update(dsAddToDima.Organizations); 
     } 

     this.Close(); 
    } 

现在是我的问题

我去服务器资源管理器 - > MyTable的(在这里我添加的记录) - >右键点击“显示表数据”,我看没有记录添加到桌子。然后我再次启动我的应用程序......在我查看MyTable之后,我新添加的记录被删除。

我无法理解这种魔法!请帮帮我!

!!!! UPDATE !!!!!

这是我的第一个形式。父窗体

namespace DimaeApplication 
{ 
    public partial class fOrganizations: Form 
    { 

     #region variables 
     fAddOrganization addOrganization; 
     fContainer container; 
     #endregion 

     public fOrganizations() 
     { 
      InitializeComponent(); 
     } 

     private void fOrganizations_Load(object sender, EventArgs e) 
     {    
      // TODO: This line of code loads data into the 'dimaeSQLDS.Organizations' table. You can move, or remove it, as needed. 
      this.organizationsTableAdapter.Fill(this.dimaeSQLDS.Organizations); 
     } 

     public void ReloadBindigs(object sender) 
     { 

      if (this.tbSearchOrganization.Text == string.Empty) 
       this.organizationsTableAdapter.Fill(this.dimaeSQLDS.Organizations); 
      else 
       try 
       { 
        string searchParam = "%" + this.tbSearchOrganization.Text + "%"; 
        this.organizationsTableAdapter.FillBy(this.dimaeSQLDS.Organizations, searchParam); 
       } 
       catch (System.Exception ex) 
       { 
        System.Windows.Forms.MessageBox.Show(ex.Message); 
       } 

     } 



     private void tsbCreateOrg_Click(object sender, EventArgs e) 
     { 
      if (addOrganization == null || addOrganization.IsDisposed) 
      { 

       addOrganization = new fAddOrganization(); 
       addOrganization.MdiParent = container ; 
       addOrganization.Show(); 
      } 
     } 

    } 
} 

我的第二形态。儿童。我在这里添加记录

namespace DimaeApplication 
{ 
    public partial class fAddOrganization : Form 
    { 
     public fAddOrganization() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      DimaeSQLDS dsAddToDima = new DimaeSQLDS(); 

     using (DimaeSQLDSTableAdapters.OrganizationsTableAdapter orgAdapter = new DimaeSQLDSTableAdapters.OrganizationsTableAdapter()) 
     { 
      orgAdapter.Fill(dsAddToDima.Organizations); 
      DimaeSQLDS.OrganizationsRow organizationsRow = dsAddToDima.Organizations.NewOrganizationsRow(); 
      organizationsRow.Address = tbINN.Text; 
      organizationsRow.OrgName = tbOrgName.Text; 
      organizationsRow.UrFiz = 0; 
      dsAddToDima.Organizations.Rows.Add(organizationsRow); //adds row to DataSet  
      this.Validate(); 
      orgAdapter.Update(dsAddToDima.Organizations); 
     } 



      this.Close(); 
     } 

     private void fAddOrganization_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      fOrganizations org = new fOrganizations();   
      org.ReloadBindigs(org); 
     }  
    } 
} 

如果您需要为每个表单生成代码,请告诉我。

谢谢你!

+0

请向我们展示您正在使用的**连接字符串**! – 2013-04-10 09:38:18

+0

难道你有某种'TRUNCATE TABLE'的东西在你用于测试的地方,并忘记删除? – 2013-04-10 09:39:06

+0

连接字符串是“数据源= |数据目录| \\ DimaeSQL.sdf” – Lena 2013-04-10 09:44:14

回答

0

实际上,当您创建使用SQL CE数据库的项目时,它会在mydocuments和驱动器c上的项目文件夹中创建两个SQL CE数据库副本。

更改您的连接字符串与SQL CE数据库的确切位置。

例子:

Data Source=C:\\DimaeSQL.sdf;Persist Security Info=False; 

问候

+0

当我从SQL表中加载数据时,我在DataGridView中获取所有记录。为什么master应该为不同的DB创建不同的连接字符串? – Lena 2013-04-10 10:12:46

+0

你说得对。一些如何我的TableAdapter从\ Projects \ DimaeApplication \ DimaeApplication \加载数据。并将数据写入\ Projects \ DimaeApplication \ DimaeApplication \ bin \ Debug \。因此,我将服务器资源管理器数据库中的连接字符串更改为\ Projects \ DimaeApplication \ DimaeApplication \ bin \ Debug。你能解释为什么会发生这种情况吗?为什么它有不同的连接字符串? – Lena 2013-04-11 05:50:46

+0

也许这是Visual Studio在创建SQL CE数据库时所做的默认设置。我甚至不知道他们为什么这么做,为什么你现在只需要指定连接字符串:) – BizApps 2013-04-11 06:20:57

0

看来你使用的是断开连接的数据集。请尝试以下操作:


Organizations table = new DimaeSQLDS.Organizations(); 
using(OrganizationsTableAdapter orgAdapter = new DimaeApplication.DimaeSQLDSTableAdapters.OrganizationsTableAdapter()) 
{ 
    orgAdapter.Fill(table); 
    DimaeSQLDS.OrganizationsRow organizationsRow = table.NewOrganizationsRow(); 
    organizationsRow.Address = tbINN.Text;    
    organizationsRow.OrgName = tbOrgName.Text;   
    dsAddToDima.Organizations.Rows.Add(organizationsRow); //adds row to DataSet 
    orgAdapter.Update(dsAddToDima.Organizations); 
} 

填充(表)是重要组成部分。即使表格为空,它也会启动对数据库的更改跟踪。

+0

我按照你的说法完成了。我已经更新了上面的代码......它没有帮助。还是一样。我可以多次重新启动应用程序,并且会显示新的数据。但是当我打开我在Sever Explorer中的表组织 - 我的新数据dissapear(((( – Lena 2013-04-11 04:49:19