예제 #1
0
파일: SimpleDemo.cs 프로젝트: JackBao/MyLab
        private static void UpdateUsingDataSet(SqlDatabase database)
        {
            var productsDataSet = new DataSet();
            var cmd = database.GetSqlStringCommand("Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products");

            // Retrieve the initial data.
            database.LoadDataSet(cmd, productsDataSet, "Products");

            // Get the table that will be modified.
            var productsTable = productsDataSet.Tables["Products"];

            // Add a new product to existing DataSet.
            productsTable.Rows.Add(new object[] { DBNull.Value, "New product", 11, 25 });

            // Modify an existing product.
            productsTable.Rows[0]["ProductName"] = "Modified product";

            // Establish the Insert, Delete, and Update commands.
            var insertCommand = database.GetStoredProcCommand("AddProduct");
            database.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
            database.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
            database.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);

            var deleteCommand = database.GetStoredProcCommand("DeleteProduct");
            database.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);

            var updateCommand = database.GetStoredProcCommand("UpdateProduct");
            database.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
            database.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
            database.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);

            // Submit the DataSet, capturing the number of rows that were affected.
            var rowsAffected = database.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand, deleteCommand,
                                UpdateBehavior.Standard);
        }