简体   繁体   English

sql更新查询不更新数据库

[英]sql update query not updating database

Im writing an application that updates a database that ive created. 我正在编写一个应用程序来更新我创建的数据库。 The application has a datagridview on it which displays the data from the database. 该应用程序具有一个datagridview,该视图显示数据库中的数据。 Something very weird is going on with my app. 我的应用程序发生了很奇怪的事情。

Here is the code that updates the database 这是更新数据库的代码

string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate WHERE      ID = @id"; 
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
{
    using (OleDbCommand updateCommand = new OleDbCommand())
    {
        updateCommand.Connection = conn;
        updateCommand.CommandText = updateCommandString;
        updateCommand.CommandType = CommandType.Text;
        updateCommand.Parameters.AddWithValue("@checkedDate", 
            this.dateTimePicker1.Value.ToShortDateString());
        updateCommand.Parameters.AddWithValue("@id", row.roomID);
        try
        {
            conn.Open();
            updateCommand.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
        }
        catch(OleDbException ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
}

Now when I run that code, close out off the app, and run the app again, the changed are correctly displayed in my datagridview that is connected to the database, but when i look at the actual database, nothing has changed at all. 现在,当我运行该代码,关闭应用程序,然后再次运行该应用程序时,所做的更改将正确显示在连接到数据库的datagridview中,但是当我查看实际的数据库时,根本没有任何更改。 I dont know why this is happening. 我不知道为什么会这样。

My sql update updates the database, which is connected to the datagrid view. 我的sql更新更新了数据库,该数据库已连接到datagrid视图。 Sow HOW is the datagrid view displaying the correct data but not the database itself. SOW HOW是显示正确数据的datagrid视图,而不显示数据库本身。

edit: I have had no sort of experience with sql before. 编辑:我以前没有使用sql的经验。

edit: transaction code: 编辑:交易代码:

 string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate WHERE ID = @id"; 
                using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
                        {
                            OleDbTransaction transaction = null;
                         using (OleDbCommand updateCommand = new OleDbCommand())
                             {

                                    updateCommand.Connection = conn;
                                    updateCommand.Transaction = transaction;    
                                    updateCommand.CommandText = updateCommandString;
                                    updateCommand.CommandType = CommandType.Text;
                                    updateCommand.Parameters.AddWithValue("@checkedDate", this.dateTimePicker1.Value.ToShortDateString());
                                    updateCommand.Parameters.AddWithValue("@id", row.roomID);
                            try
                            {
                                conn.Open();
                                transaction = conn.BeginTransaction();
                                updateCommand.ExecuteNonQuery();
                                transaction.Commit();

                                conn.Close();
                                conn.Dispose();
                            }
                            catch(OleDbException ex)
                            {
                                MessageBox.Show(ex.Message.ToString());
                            }
                        }
                    }

Possible reasons: 可能的原因:

  1. Your application may connect to copy of database file. 您的应用程序可能连接到数据库文件的副本。 Search your project directory for copies of database (.accdb extension?) 在项目目录中搜索数据库副本(扩展名为.accdb?)
  2. Maybe David S is right, but you have to commit changes on database ( OleDbTransaction help at MSDN ). 也许David S是正确的,但是您必须提交数据库更改( MSDN上的OleDbTransaction帮助 )。 Changing isolation settings to READ UNCOMMITTED is inelegant approach to problem. 将隔离设置更改为READ UNCOMMITTED是解决问题的有效方法。

Sounds like you may have a transaction isolation issue to me. 听起来您可能遇到了我的交易隔离问题。 If you are unfamiliar with this topic, suggest you google & read the docs. 如果您不熟悉此主题,建议您使用google并阅读文档。 I believe READ COMMITTED is the default ... Setting it to READ UNCOMMITTED probably fixes issue for you not being able to see the updates. 我认为READ COMMITTED是默认的...将其设置为READ UNCOMMITTED可能会解决您无法看到更新的问题。 But, this is not really a recommended approach. 但是,这实际上不是推荐的方法。 In general, you will just want to make sure that all of your transactions are committed to the database before trying to inspect the information. 通常,您只需要确保在尝试检查信息之前将所有事务都提交给数据库即可。

Additional note: I've had need from time to time during testing an application to want to be able to query the db from a SQL tool outside of my application to check on something. 附加说明:在测试应用程序时,我有时需要能够从应用程序外部的SQL工具查询数据库以进行检查。 However, in almost 15 years of developing db applications, I've never had need to set this to READ UNCOMMITTED for production. 但是,在将近15年的数据库应用程序开发中,我从来没有必要将其设置为“读取未提交”以进行生产。 I would be very cautious about going to production on anything other than READ COMMITTED. 除了将“ READ COMMITTED”(读取提交)以外的任何内容用于生产,我将非常谨慎。 I'm not saying there is never a case where you would need to want that, but it's not the norm (IMHO). 我并不是说永远不会有需要的情况,但这不是规范(IMHO)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM