0

再会!

我是 C# 的新手。

我在 Windows 窗体中有数据集,并且已向数据集添加了一条记录。然后我调用 TableAdapter.Update(MyTable) (RowState is in added mode) 。进行了更改,我可以在绑定到 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 后,我新添加的记录被删除了。

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

!!!!更新!!!!!

这是我的第一个表格。父表格

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);
        }      
    }
}

告诉我您是否需要为每个表单生成代码。

还是非常感谢!!

4

2 回答 2

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);
}

Fill(table) 是重要的部分。即使表是空的,它也会启动对数据库的更改跟踪。

于 2013-04-10T10:32:42.403 回答
0

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

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

例子 :

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

此致

于 2013-04-10T10:00:18.327 回答