SQL Server,Datagrid:从中选择,更新所有表中的字段

时间:2017-02-02 14:06:04

标签: c# sql sql-server datagridview

我想更新我的sql server中的所有表,这些表在rowchanged事件上有一个带有datagrid的特定字段。

我还考虑过使用父键和子键,但是最小的2个值决定了项的唯一性。除此之外,主表必须具有重复值,只有1列可以识别从表上的位置。

项目>物品位置(物品可以吐痰和物理分配

或者我考虑使用命令构建器,但我想不出用它做这个的逻辑方法。

但是我不确定该声明会如何发生.. 这是想法:

这也必须包括主表上的所有值。

UPDATE "alltables" SET " + iOne + "," + iTwo + " WHERE column1="previous entry" 

我是如何接近它的:

 private void button1_Click_1(object sender, EventArgs e)
{
    string iOne = dgMasterGridView.SelectedRows[0].Cells[1].Value + string.Empty;
    string iTwo = dgMasterGridView.SelectedRows[0].Cells[2].Value + string.Empty;
    string iThree = dgMasterGridView.SelectedRows[0].Cells[3].Value + string.Empty;

    string slaveiOne = dgSlave1GridView.SelectedRows[0].Cells[1].Value + string.Empty;
    string slaveiTwo = dgSlave1GridView.SelectedRows[0].Cells[2].Value + string.Empty;
    string slaveiThree = dgSlave1GridView.SelectedRows[0].Cells[3].Value + string.Empty;

    try
        {
            using (SqlConnection con = new SqlConnection(Connection.MTRDataBaseConn))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();

        // ???????????????  UPDATE STATEMENT  ???????????????
        cmd.CommandText = "UPDATE "alltables" SET " + iOne + "," + iTwo + " WHERE column1="previous entry";

        cmd.Connection = con;

        cmd.Parameters.AddWithValue("@Item1", iOne);
        cmd.Parameters.AddWithValue("@Item2", iTwo);
        cmd.Parameters.AddWithValue("@Item3", iThree);

        cmd.ExecuteNonQuery();

        MessageBox.Show("Information has been submitted");

    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

1 个答案:

答案 0 :(得分:1)

你可以用两种方式做到:

1-通过逐个更新表的存储过程,您需要将新值作为参数发送...

2到.net代码如果你有表的数据集,你可以只使用.Update()....来使用SQLDataAdapter。

此更新;我在我的代码上使用此函数来使用数据适配器使用begin transaction,commit和roolback更新多个数据集时出错..希望这对您有所帮助......

    public bool Update_DS(ArrayList strTableName, ArrayList objSqlDataAdapter, ArrayList objDataSet)
    {
        bool ErrorOccured = false;
        try
        {
            _LastException = null;
            objSqlTransaction = objSqlConnection.BeginTransaction();

            for (int i = 0; i < objSqlDataAdapter.Count; i++)
            {
                SqlDataAdapter objDATemp = (SqlDataAdapter)objSqlDataAdapter[i];

                //objDATemp.SelectCommand = new SqlCommandBuilder(objDATemp).getse();
                objDATemp.SelectCommand.Transaction = objSqlTransaction;
                objDATemp.SelectCommand.Connection = objSqlConnection;

                objDATemp.UpdateCommand = new SqlCommandBuilder(objDATemp).GetUpdateCommand();
                objDATemp.UpdateCommand.Transaction = objSqlTransaction;
                objDATemp.UpdateCommand.Connection = objSqlConnection;

                objDATemp.InsertCommand = new SqlCommandBuilder(objDATemp).GetInsertCommand();
                objDATemp.InsertCommand.Transaction = objSqlTransaction;
                objDATemp.InsertCommand.Connection = objSqlConnection;

                objDATemp.DeleteCommand = new SqlCommandBuilder(objDATemp).GetDeleteCommand();
                objDATemp.DeleteCommand.Transaction = objSqlTransaction;
                objDATemp.DeleteCommand.Connection = objSqlConnection;

                objDATemp.Update((DataSet)objDataSet[i], strTableName[i].ToString());
            }
            return true;

        }
        catch (Exception ex)
        {
            _LastException = ex;
            ErrorOccured = true;
            //OnError(ex);
            return false;
        }
        finally
        {
            if (ErrorOccured)
                objSqlTransaction.Rollback();
            else
                objSqlTransaction.Commit();

            if (objSqlCommand != null)
            {
                objSqlCommand.Dispose();
                objSqlCommand = null;
            }
        }
    }