DataSet
. In that case, the wizard will generate the UpdateCommand
automatically 如果 it is able to do so. If it wasn't able to do so in your case then that is most likely because one or more of your tables doesn't have a primary key. With no primary key, there's no obvious way to uniquely identify a record to update or delete, thus no UpdateCommand
or DeleteCommand
can be generated. It's a very rare thing that not having a primary key in a database table is a good idea. You should fix that and then regenerate the typed DataSet
in the Data Sources window to have those commands automatically generated.使用系统;
使用System.Configuration;
using System.Data;
使用System.Data.SqlClient;
使用System.Windows.Forms;
namespace Payroll
{
公共局部类Form1:表单
{
public Form1()
{
InitializeComponent();
}
private SqlConnection con =新的SqlConnection(ConfigurationManager.ConnectionStrings ["con"].ConnectionString);
private SqlCommand命令=新的SqlCommand();
私有void Form1_Load(对象发送者,EventArgs e)
{
// TODO:这行代码将数据加载到“ salariesDataSet.Salaries”表中。您可以根据需要移动或删除它。
this.salariesTableAdapter.FillBy(this.salariesDataSet.Salaries);
}
//声明变量
private 十进制BasicSalary,加班,ExtraOvertime,LeaveOutstanding,GrossSalary,SSNIT13,SSNIT5,应纳税收入,IncomeTax,缺勤,TotalDeductions,NetSalary;
private int DaysWorked,DaysAbsent;
private DateTime EmpDate,CurrentDate;
私人void saveButton_Click(object sender,EventArgs e)
{
this.Validate();
this.salariesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.salariesDataSet);
}
私人void salariesBindingNavigatorSaveItem_Click(对象发送者,EventArgs e)
{
try
{
this.Validate();
this.salariesBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.salariesDataSet);
SalariesDataSet.SalariesDataTable deleteSalaries =(SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Deleted);
SalariesDataSet.SalariesDataTable newSalaries =(SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Added);
SalariesDataSet.SalariesDataTablemodifiedSalaries =(SalariesDataSet.SalariesDataTable)
SalariesDataSet.SalariesRow.GetChanges(DataRowState.Modified);
{
//从薪金表中删除所有已删除的薪水。
如果(deletedSalaries!= null)
{
salariesTableAdapter.Update(deletedSalaries);
}
//将新的薪水添加到Salaries表中。
如果(newSalaries!= null)
{
salariesTableAdapter.Update(newSalaries);
}
//更新所有修改后的薪水。
如果(modifiedSalaries!= null)
{
salariesTableAdapter.Update(newSalaries);
}
salariesDataSet.AcceptChanges();
}
}
捕获(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}