我从数据库拉出一张DataTable(由五张表组成),将它绑定DataView后作为DataGridView的DataSource,我在界面上进行添加,修改,删除后,怎么样实现更新五张表(五张表有相同字段EmployerID)
我用自己构造的updateCimmand,insertCommand,deleteCommamd,一张一张表更新,但是智能更新第一张,这是为什么,请高手指教
SqlConnection conn = new SqlConnection(SQL_Conn);
SqlDataAdapter da = new SqlDataAdapter();
conn.Open(); //////////////////////////////////////////////////////////////////////////////////////////////////
//为更新数据库做准备,构造updateCimmand,insertCommand,deleteCommamd
da.UpdateCommand = new SqlCommand(SQL_update_EmployerInfo, conn);
da.UpdateCommand.Parameters.Add("@EmployerName", SqlDbType.VarChar, 8, "EmployerName");
da.UpdateCommand.Parameters.Add("@Sex", SqlDbType.VarChar, 2, "Sex");
da.UpdateCommand.Parameters.Add("@Age", SqlDbType.VarChar, 2, "Age");
da.UpdateCommand.Parameters.Add("@DepartmentID", SqlDbType.VarChar, 2, "DepartmentID");
da.UpdateCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID"); da.InsertCommand = new SqlCommand(SQL_insert_EmployerInfo, conn);
da.InsertCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
da.InsertCommand.Parameters.Add("@EmployerName", SqlDbType.VarChar, 8, "EmployerName");
da.InsertCommand.Parameters.Add("@Sex", SqlDbType.VarChar, 2, "Sex");
da.InsertCommand.Parameters.Add("@Age", SqlDbType.VarChar, 2, "Age");
da.InsertCommand.Parameters.Add("@DepartmentID", SqlDbType.VarChar, 2, "DepartmentID"); da.DeleteCommand = new SqlCommand(SQL_delete_EmployerInfo, conn);
da.DeleteCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
//////////////////////////////////////////////////////////////////////////////////////////////////
//更新EmployerInfo
try
{
da.Update(dt);
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.ToString(), "警告");
}
try
{
da.UpdateCommand = new SqlCommand(SQL_update_EmployerTypeInfo, conn);
da.UpdateCommand.Parameters.Clear();
da.UpdateCommand.Parameters.Add("@WorkTypeID", SqlDbType.VarChar, 2, "WorkTypeID");
da.UpdateCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID");
da.InsertCommand = new SqlCommand(SQL_insert_EmployerTypeInfo, conn);
da.InsertCommand .Parameters.Clear();
da.InsertCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID");
da.InsertCommand.Parameters.Add("@WorkTypeID", SqlDbType.VarChar, 2, "WorkTypeID");
da.DeleteCommand = new SqlCommand(SQL_delete_EmployerTypeInfo, conn);
da.DeleteCommand .Parameters.Clear();
da.DeleteCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
da.Update(dt);
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.ToString(), "警告");
} conn.Close();
我用自己构造的updateCimmand,insertCommand,deleteCommamd,一张一张表更新,但是智能更新第一张,这是为什么,请高手指教
SqlConnection conn = new SqlConnection(SQL_Conn);
SqlDataAdapter da = new SqlDataAdapter();
conn.Open(); //////////////////////////////////////////////////////////////////////////////////////////////////
//为更新数据库做准备,构造updateCimmand,insertCommand,deleteCommamd
da.UpdateCommand = new SqlCommand(SQL_update_EmployerInfo, conn);
da.UpdateCommand.Parameters.Add("@EmployerName", SqlDbType.VarChar, 8, "EmployerName");
da.UpdateCommand.Parameters.Add("@Sex", SqlDbType.VarChar, 2, "Sex");
da.UpdateCommand.Parameters.Add("@Age", SqlDbType.VarChar, 2, "Age");
da.UpdateCommand.Parameters.Add("@DepartmentID", SqlDbType.VarChar, 2, "DepartmentID");
da.UpdateCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID"); da.InsertCommand = new SqlCommand(SQL_insert_EmployerInfo, conn);
da.InsertCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
da.InsertCommand.Parameters.Add("@EmployerName", SqlDbType.VarChar, 8, "EmployerName");
da.InsertCommand.Parameters.Add("@Sex", SqlDbType.VarChar, 2, "Sex");
da.InsertCommand.Parameters.Add("@Age", SqlDbType.VarChar, 2, "Age");
da.InsertCommand.Parameters.Add("@DepartmentID", SqlDbType.VarChar, 2, "DepartmentID"); da.DeleteCommand = new SqlCommand(SQL_delete_EmployerInfo, conn);
da.DeleteCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
//////////////////////////////////////////////////////////////////////////////////////////////////
//更新EmployerInfo
try
{
da.Update(dt);
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.ToString(), "警告");
}
try
{
da.UpdateCommand = new SqlCommand(SQL_update_EmployerTypeInfo, conn);
da.UpdateCommand.Parameters.Clear();
da.UpdateCommand.Parameters.Add("@WorkTypeID", SqlDbType.VarChar, 2, "WorkTypeID");
da.UpdateCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID");
da.InsertCommand = new SqlCommand(SQL_insert_EmployerTypeInfo, conn);
da.InsertCommand .Parameters.Clear();
da.InsertCommand.Parameters.Add("@EmployerID", SqlDbType.Char, 8, "EmployerID");
da.InsertCommand.Parameters.Add("@WorkTypeID", SqlDbType.VarChar, 2, "WorkTypeID");
da.DeleteCommand = new SqlCommand(SQL_delete_EmployerTypeInfo, conn);
da.DeleteCommand .Parameters.Clear();
da.DeleteCommand.Parameters.Add("@EmployerID", SqlDbType.VarChar, 8, "EmployerID");
da.Update(dt);
}
catch (Exception ee)
{
System.Windows.Forms.MessageBox.Show(ee.ToString(), "警告");
} conn.Close();
解决方案 »
- 可访问性不一致: 属性类型“ ”比属性“ ”的可访问性低
- C#如何获取有自动编号的列
- IDictionary 序列化错误
- 如何判断datagridview的单元格是否为空?
- C# ORACLE执行insert,数据库并没有改变,可能是什么原因呢?急求!~~
- 想让一个label在form窗体上间隔一段时间闪烁一次隔一段时间在闪烁一次
- 相对路径的问题
- 。net2005 上报表测试正常,发布就出现登录失败。详细资料: Error Code: 0x Source: ADODB.Connection Descrip
- c#遍历xml怎么只显示一条记录
- 呵呵,简单问题
- 关闭页面事件~~,会员问题
- 做一个监控电脑操作的软件
一个DataTable由五张表组成啥意思?
DataSet里放五个DataTable吧
只能手动更新了
那个Update只能针对单表操作