我一直在从事一个使用表适配器和数据集来访问访问数据库的项目。我即将完成并从 Visual Studio 运行代码分析,它提出了一些关于需要在我使用这些方法的特定类上实现 IDisposable 的错误。虽然我已经查看了一些关于使用 dispose 方法或使用 using 块的不同内容,但我不确定如何使其工作。数据集和表适配器被创建为供整个类使用的全局变量,许多类将调用其他类,这些类也将使用不同的表适配器和数据集。我试过创建一个 Dispose 方法,但我不知道什么时候调用它,我想知道是否在错误的时间调用它会导致我的程序崩溃。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace recipeDataBase
{
class NewRecipe : IDisposable
{
recipiesNewDataSet recipeDataSet = new recipiesNewDataSet();
recipiesNewDataSetTableAdapters.RecipeTableAdapter recipeTableAdapter = new recipiesNewDataSetTableAdapters.RecipeTableAdapter();
recipiesNewDataSetTableAdapters.RecipeIngredientTableAdapter recipeIngredientTableAdapter = new recipiesNewDataSetTableAdapters.RecipeIngredientTableAdapter();
recipiesNewDataSetTableAdapters.RatingTableAdapter ratingTableAdapter = new recipiesNewDataSetTableAdapters.RatingTableAdapter();
recipeDataBase.recipiesNewDataSetTableAdapters.IngredientTableAdapter ingredientTableAdapter = new recipiesNewDataSetTableAdapters.IngredientTableAdapter();
private RecipeInfo newRecipe;
private RatingNum originalRatingNum;
private RatingNum newRating;
private RecipeInfo originalRecipe;
private string[] ingredients;
public NewRecipe(RecipeInfo incommingNewRecipe, RatingNum IncommingNewRating, string[] incommingIngredients)
{
newRecipe = incommingNewRecipe;
newRating = IncommingNewRating;
ingredients = incommingIngredients;
CreateNewRecipe();
UpdateNewRecipe();
}
public void CreateNewRecipe()
{
originalRatingNum = new RatingNum();
originalRecipe = new RecipeInfo();
originalRatingNum.cookingTime = 0;
originalRatingNum.easeOfCooking = 0;
originalRatingNum.familyRating = 0;
originalRatingNum.healthRating = 0;
originalRatingNum.userRating = 0;
ratingTableAdapter.Fill(recipeDataSet.Rating);
ratingTableAdapter.Insert(originalRatingNum.userRating, originalRatingNum.familyRating, originalRatingNum.healthRating, originalRatingNum.easeOfCooking, originalRatingNum.cookingTime);
Query getNewRecipeNumbers = new Query();
int newRatingNumber = getNewRecipeNumbers.newRatingNum();
originalRatingNum.ratingNum = newRatingNumber;
newRating.ratingNum = newRatingNumber;
newRecipe.ratingNum = newRatingNumber;
originalRecipe.recipeName = "newRecipe";
originalRecipe.nationality = "newRecipe";
originalRecipe.recipeEvent = "newRecipe";
originalRecipe.source = "newRecipe";
originalRecipe.type = "newRecipe";
originalRecipe.servings = "0";
originalRecipe.ratingNum = newRatingNumber;
recipeTableAdapter.Fill(recipeDataSet.Recipe);
recipeTableAdapter.Insert(originalRecipe.recipeName, originalRecipe.nationality, originalRecipe.recipeEvent, originalRecipe.source, originalRecipe.type, originalRecipe.servings, originalRecipe.ratingNum);
int newRecipeNum = getNewRecipeNumbers.newRecipeNum();
newRecipe.recipeNum = newRecipeNum;
originalRecipe.recipeNum = newRecipeNum;
recipeDataSet.AcceptChanges();
}
public void UpdateNewRecipe()
{
UpdateRatingNum updateRatingNum = new UpdateRatingNum(originalRatingNum, newRating);
UpdateRecipe updateRecipe = new UpdateRecipe(newRecipe, originalRecipe);
UpdateIngredients updateIngredients = new UpdateIngredients(ingredients);
UpdateRecipeIngredient updateRecpeIngredients = new UpdateRecipeIngredient(ingredients, newRecipe.recipeNum);
recipeDataSet.AcceptChanges();
}
public void Dispose()
{
ratingTableAdapter.Dispose();
recipeTableAdapter.Dispose();
recipeTableAdapter.Dispose();
ingredientTableAdapter.Dispose();
recipeDataSet.Dispose();
throw new NotImplementedException();
}
}
}
如您所见,我确实实现了 Idisposable 并使用自动创建的方法将所有表适配器和数据集放入其中以进行处置,但是我如何使用它以及在哪里使用它?
谢谢您的帮助
克雷格