2011-07-26 73 views
10

我确信有一个非常简单的理由,这一行不起作用,但它在过去的一周已经回避,所以我希望别人会注意到我的错。DataAdapter.Update()不更新数据库

我一直在为这个项目工作几个星期到一个月。我一直在使用旧的DataAdapter,CommandBuiler等与一些LINQ到一个数据库上的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"); 

我没有得到任何错误,但数据表刚刚获得” t更新。

在此先感谢您的帮助,只需告知我您是否需要更多信息。

+0

你有没有得到答案呢?我与我为处理Db更新而构建的项目有同样的问题。从一个项目中使用它,就像你注意到的那样失败。从另一个使用它,它工作正常。所以我真的很难过。我甚至比较了两者中的SqlAdapter.GetUpdateCommand()。CommandText,它们与ItemArray数据元素完全相同。不过,我并不完全了解自动生成的INSERT命令中的所有参数。 RowStates也是一样的。令人沮丧。 –

回答

2

更新的SqlCommand是什么样的?我看到命令,但没有看到任何SqlText,那就是你错过的东西。

您需要通过在SqlDataAdapter

这个链接给出了如何去做一个很好的细分设置.UpdateCommand属性来定义什么.Update做: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx

+0

SqlText?我会在哪里放? – Miffed

+0

谢谢。但是,我将如何在我的代码中使用它? – Miffed

11

为了更新的数据数据库你的SqlDataAdapter需要有它的InsertCommand,UpdateCommand,DeleteCommand属性集。您创建的SqlCommandBuilder实例具有这些命令,但您需要将它们设置为您的SqlDataAdapter。

在其他的世界:某处

SqlCommandBuilder cb; 
cb = new SqlCommandBuilder(dataAdapt); 

dataAdapt.Update(dataRecipe, "CookBookRecipes"); 

之间需要

dataAdapt.DeleteCommand = cb.GetDeleteCommand(true); 
dataAdapt.UpdateCommand = cb.GetUpdateCommand(true); 
dataAdapt.InsertCommand = cb.GetInsertCommand(true); 
+2

遍及搜索,没有人提到将命令构建器生成的查询绑定到数据适配器。谢谢你。 – Ajibola

+1

请注意,如果您在创建SqlCommandBuilder时指定了dataAdaptor,则不需要设置命令,SqlCommandBuilder将自己注册为侦听器。 [文档](https://msdn.microsoft.com/en-us/library/w0154tsb(V = vs.110)的.aspx) – nicV

0

您可能需要

DataAdapeter.AcceptChanges() 
0

我遇到了同样的问题:使用一些新行填充新的数据集,但更新时没有发生任何事情。我使用了类似的MySqlDataAdapter。

事实证明,当您需要MySqlCommandBuilder中的InsertCommand时,您必须指定添加的行状态。另请参见:MSDN

0
//change this line 

DataRow daRow = dataRecipe.Tables["CookBookRecipes"].NewRow(); 

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) 
{ 
//add & change this too 
dataRecipe.Tables["CookBookRecipes"].Rows.Add(daRow); 
    dataAdapt.Update(dataRecipe, "CookBookRecipes"); 

    MessageBox.Show("Recipe Updated", "Update"); 
} 

}

0

尝试下面的来源。

private void btnSave_Click(object sender, EventArgs e) 
{ 
    cb = new SqlCommandBuilder(dataAdapt); 

    //Old source: DataRow daRow = dataRecipe.Tables["CookBookRecipes"].Rows[inc]; 

    //Added source code 
    DataRow daRow = dataRecipe.Tables["CookBookRecipes"].NewRow(); 

    //Added source code 
    dataRecipe.Tables["CookBookRecipes"].AddRow(daRow); 

    daRow.BeginEdit(); 
    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; 
    daRow.EndEdit(); 

    //Reset state of rows to unchanged 
    dataRecipe.Tables["CookBookRecipes"].AcceptChanges(); 
    //Set modified. The dataAdapt will call update stored procedured 
    //for the row that has Modifed row state. 
    //You can also try SetAdded() method for new row you want to insert 
    daRow.SetModified(); 

    if (MessageBox.Show("You wish to save your updates?", "Save Updates?", MessageBoxButtons.OKCancel) == DialogResult.OK) 
    { 

     dataAdapt.Update(dataRecipe, "CookBookRecipes"); 

     MessageBox.Show("Recipe Updated", "Update"); 
    } 
} 
0

更新之前添加AcceptChangesDuringUpdate对我的作品,例如:

foreach (string tableName in tableNames) 
     {    
      da = new SqlDataAdapter("SELECT * FROM " + tableName, cn); 
      cb = new SqlCommandBuilder(da); //initialise the update, insert and delete commands of da 
      da.AcceptChangesDuringUpdate = true; 
      da.Update(myDataSet, tableName);    
     } 
0

我也遇到了同样的问题。我的dataadapter.fill工作,但dataadapter.update不起作用。我意识到问题是我的数据库表不包含主键。在修改我的表格以包含具有主键的列后,dataadapter.fill将起作用。 希望这可以帮助别人。