再会!
我是 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);
}
}
}
告诉我您是否需要为每个表单生成代码。
还是非常感谢!!