13

この 1 行が機能しないのは非常に単純な理由があると確信していますが、この 1 週間は回避されているので、他の誰かが私の過ちに気付くことを願っています。

私はこのプロジェクトに数週間から 1 か月間取り組んできました。私は古いDataAdapter、CommandBuilerなどを、複数のWindowsアプリケーションフォームを使用して、1つのデータベースでいくつかのlinq to sqlコーディングと組み合わせて使用​​しています。この特定のフォームは、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");

エラーは発生していませんが、データ テーブルが更新されません。

ご協力いただきありがとうございます。さらに情報が必要な場合はお知らせください。

4

10 に答える 10

13

データベースのデータを更新するには、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);
于 2012-10-16T12:56:13.930 に答える
2

SqlCommandfor Update はどのように見えますか? コマンドは表示されますが、SqlText が表示されません。それが不足しています。

プロパティを.Update設定して、何をするかを定義する必要があります.UpdateCommandSqlDataAdapter

このリンクは、それを行う方法のかなり良い内訳を提供します: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx

于 2011-07-26T16:33:23.400 に答える
0

私は同じ問題に遭遇しました。私の dataadapter.fill は機能しますが、dataadapter.update は機能しません。データベース テーブルに主キーが含まれていないことが問題であることに気付きました。主キーを持つ列を含めるようにテーブルを変更した後、dataadapter.fill が機能します。これが誰かに役立つことを願っています。

于 2017-12-15T08:39:40.220 に答える
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");
}

}

于 2015-02-18T13:44:31.367 に答える