我敢肯定这条线没有工作的原因非常简单,但过去一周它已经回避了,所以我希望其他人会注意到我的错。
我已经在这个项目上工作了几个星期到一个月。我一直在使用旧的 DataAdapter、CommandBuiler 等,在 1 个数据库上使用一些 linq to sql 编码,以及多个 Windows 应用程序表单。此特定表单使用 DataAdapter、Dataset 和 Command Builder 从数据库中编辑或删除行。它一直工作正常,直到我换了电脑。现在数据集正在更新,但数据库没有。
这是此表单的完整代码:
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Exit Cook Book?", "Exit?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
private void goBackToolStripMenuItem_Click(object sender, EventArgs e)
{
AddRecipe goBack = new AddRecipe();
Close();
goBack.Show();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Scan through the Cook Book to find recipes that you wish to edit or delete.", "Help!");
}
SqlConnection con;
SqlDataAdapter dataAdapt;
DataSet dataRecipe;
SqlCommandBuilder cb;
int MaxRows = 0;
int inc = 0;
private void EditRecipe_Load(object sender, EventArgs e)
{
con = new SqlConnection();
dataRecipe = new DataSet();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Recipes.mdf;Integrated Security=True;User Instance=True";
con.Open();
//MessageBox.Show("Database Open");
string sql = "SELECT* From CookBookRecipes";
dataAdapt = new SqlDataAdapter(sql, con);
dataAdapt.Fill(dataRecipe, "CookBookRecipes");
NavigateRecords();
MaxRows = dataRecipe.Tables["CookBookRecipes"].Rows.Count;
con.Close();
}
private void NavigateRecords()
{
DataRow dRow = dataRecipe.Tables["CookBookRecipes"].Rows[inc];
tbRName.Text = dRow.ItemArray.GetValue(0).ToString();
listBox1.SelectedItem = dRow.ItemArray.GetValue(1).ToString();
tbRCreate.Text = dRow.ItemArray.GetValue(2).ToString();
tbRIngredient.Text = dRow.ItemArray.GetValue(3).ToString();
tbRPrep.Text = dRow.ItemArray.GetValue(4).ToString();
tbRCook.Text = dRow.ItemArray.GetValue(5).ToString();
tbRDirections.Text = dRow.ItemArray.GetValue(6).ToString();
tbRYield.Text = dRow.ItemArray.GetValue(7).ToString();
textBox1.Text = dRow.ItemArray.GetValue(8).ToString();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("That's the last recipe of your Cook Book!", "End");
}
}
private void btnBack_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("This is the first recipe of your Cook Book!", "Start");
}
}
private void btnSave_Click(object sender, EventArgs e)
{
cb = new SqlCommandBuilder(dataAdapt);
DataRow daRow = dataRecipe.Tables["CookBookRecipes"].Rows[inc];
daRow[0] = tbRName.Text;
daRow[1] = listBox1.SelectedItem.ToString();
daRow[2] = tbRCreate.Text;
daRow[3] = tbRIngredient.Text;
daRow[4] = tbRPrep.Text;
daRow[5] = tbRCook.Text;
daRow[6] = tbRDirections.Text;
daRow[7] = tbRYield.Text;
daRow[8] = textBox1.Text;
if (MessageBox.Show("You wish to save your updates?", "Save Updates?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
dataAdapt.Update(dataRecipe, "CookBookRecipes");
MessageBox.Show("Recipe Updated", "Update");
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb;
cb = new SqlCommandBuilder(dataAdapt);
if (MessageBox.Show("You wish to DELETE this recipe?", "Delete?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
dataRecipe.Tables["CookBookRecipes"].Rows[inc].Delete();
MaxRows--;
inc = 0;
NavigateRecords();
dataAdapt.Update(dataRecipe, "CookBookRecipes");
MessageBox.Show("Your Recipe has been Deleted", "Delete");
}
}
这应该更新表:
dataAdapt.Update(dataRecipe, "CookBookRecipes");
我没有收到任何错误,但数据表不会更新。
提前感谢您的帮助,如果您需要更多信息,请告诉我。