更改未使用 SqlDataAdapter 保存到数据库

本文关键字:数据库 保存 SqlDataAdapter 未使用 | 更新日期: 2023-09-27 18:31:57

我被困在无法保存到SQL Server数据库中的更改中。

我可以正确读取数据库,并且在调用从数据库刷新的RefreshGrid()时,网格视图会正确更新。但是在重新启动程序或检查实际数据库时则不然,则更改消失了。

我通常会为此使用实体框架,但有时需要学习其他方法。

private string conString = ConfigurationManager.ConnectionStrings["RewardsConnectionString"].ConnectionString;
private RewardsDS rewardsDset = new RewardsDS();
private SqlDataAdapter CreateDataAdapter(SqlConnection conn)
{
    // Build the selection query.
    SqlDataAdapter rewardsAdapter = new SqlDataAdapter();
    SqlCommand selectCommand = new SqlCommand("SELECT * FROM Purchases", conn);
    rewardsAdapter.SelectCommand = selectCommand;
    // Build the insertion query
    SqlCommand insertCommand = new SqlCommand(
        @"INSERT INTO Purchases (PurchaseDate, Amount, CustomerId)
        VALUES (@PurchaseDate, @Amount, @CustomerId)", conn);
    insertCommand.Parameters.Add("@PurchaseDate", SqlDbType.DateTime, 0, "PurchaseDate"); // Not sure about this zero ...
    insertCommand.Parameters.Add("@Amount", SqlDbType.Decimal, 0, "Amount");
    insertCommand.Parameters.Add("@CustomerId", SqlDbType.Int, 0, "CustomerId");
    rewardsAdapter.InsertCommand = insertCommand;
    // Build the update query
    SqlCommand updateCommand = new SqlCommand(
        @"UPDATE Purchases SET Amount = @Amount, PurchaseDate = @PurchaseDate WHERE PurchaseId = @PurchaseId", conn);
    updateCommand.Parameters.Add("@Amount", SqlDbType.Decimal, 0, "Amount");
    updateCommand.Parameters.Add("@PurchaseDate", SqlDbType.DateTime, 0, "PurchaseDate");
    SqlParameter param = updateCommand.Parameters.Add("@PurchaseId", SqlDbType.Int, 0, "PurchaseId");
    param.SourceVersion = DataRowVersion.Original;
    rewardsAdapter.UpdateCommand = updateCommand;
    // Build the delete query
    SqlCommand deleteCommand = new SqlCommand(
        "DELETE FROM Purchases WHERE PurchaseId = @PurchaseId", conn);
    param = deleteCommand.Parameters.Add("@Id", SqlDbType.Int, 0, "PurchaseId");
    param.SourceVersion = DataRowVersion.Original;
    rewardsAdapter.DeleteCommand = deleteCommand;
    return rewardsAdapter;
}
// Gets changes and saves them to the database.
private void SaveChanges()
{
    using (SqlConnection conn = new SqlConnection(conString))
    {
        var fadapter = CreateDataAdapter(conn);
        conn.Open();
        DataSet changes = rewardsDset.GetChanges();
        try
        {
            fadapter.Update(changes, "Purchases");
        }
        catch (Exception ex)
        {
            MessageBox.Show("There are no updates to be saved");
        }
        conn.Close();
        rewardsDset.AcceptChanges();
    }
}
// Refresh the dataGridView from the database
private void RefreshGrid()
{
    rewardsDset.Clear();
    using (SqlConnection conn = new SqlConnection(conString))
    {
        conn.Open();
        SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Purchases", conn);
        myAdapter.Fill(rewardsDset, "Purchases");
        conn.Close();
        dataGridView1.DataSource = null;
        this.dataGridView1.DataSource = rewardsDset.Tables["Purchases"];
    }
}

更改未使用 SqlDataAdapter 保存到数据库

你只需要执行查询:

 rewardsAdapter.InsertCommand.ExecuteNonQuery();

首先,我将为浪费任何人的时间道歉。终于找到了导致此错误的原因,它比我想象的要基本得多,而且根本不是我一直在寻找解决方案的地方。我在连接字符串方面犯了一个令人尴尬的错误,但如果有人重复我的错误,那就是它:

我一直在使用数据集的连接字符串,而不是实际数据库的连接字符串......

添加正确的连接字符串肯定解决了我的问题,当然,仅使用我上面发布的代码任何人都无法看到。