我有一个非常简单(但很奇怪)的问题。我构建了一个简单的程序,它接收数据并将其存储在SQL数据库中。我的代码如下。我的表单上有一个列表框,向我显示我通过按钮添加的数据,即使在我关闭程序,重新启动计算机之后,这些数据仍然存在,等等。问题是,我不能(对于我的生活)找到这些数据的存储位置。我在数据库浏览器下检查了所有内容,并且没有任何新内容(除了我手动输入的3个原始内容)条目。有什么建议?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Cookbook
{
public partial class frmMain : Form
{
SqlConnection connection;
string connectionString;
public frmMain()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["Cookbook.Properties.Settings.CookbookConnectionString"].ConnectionString;
PopulateRecipes();
}
private void PopulateRecipes()
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection))
{
DataTable recipeTable = new DataTable();
adapter.Fill(recipeTable);
lstRecipes.DisplayMember = "Name";
lstRecipes.ValueMember = "Id";
lstRecipes.DataSource = recipeTable;
}
}
private void PopulateIngredients()
{
string query = "SELECT a.Name FROM Ingredient a " +
"INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId " +
"WHERE b.RecipeId = @RecipeId";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);
DataTable IngredientTable = new DataTable();
adapter.Fill(IngredientTable);
lstIngredients.DisplayMember = "Name";
lstIngredients.ValueMember = "Id";
lstIngredients.DataSource = IngredientTable;
}
}
private void lstRecipes_SelectedIndexChanged(object sender, EventArgs e)
{
PopulateIngredients();
}
private void addbutton_Click(object sender, EventArgs e)
{
string query = "INSERT INTO Recipe VALUES (@RecipeName, 80, 'blah blah')";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@RecipeName", comboBox1.Text);
command.ExecuteScalar();
}
PopulateRecipes();
}
private void frmMain_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cookbookDataSet.RecipeIngredient' table. You can move, or remove it, as needed.
this.recipeIngredientTableAdapter.Fill(this.cookbookDataSet.RecipeIngredient);
// TODO: This line of code loads data into the 'cookbookDataSet.Recipe' table. You can move, or remove it, as needed.
this.recipeTableAdapter.Fill(this.cookbookDataSet.Recipe);
}
}
}
当我通过数据库浏览器查看时,我的“成分”表中只有生菜,番茄,奶酪 - >显示表数据,但是当我查看表单中的数据时,它会显示我之前添加的所有数据。