插入未提交到数据库 [英] Inserting not committed to database

查看:196
本文介绍了插入未提交到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些困难让我的DataSet工作。



我在后台有一个MDB数据库,并创建了一个DataSet。现在我创建了一个新的方法,让我在表中创建一个新用户。



但是当我打电话给它时,没有任何反应。没有例外,没有错误,我甚至得到1作为受影响的行数返回。但是,当我查看数据库时,没有添加任何用户。



我有一种感觉,我想知道DataSet,我想要操作数据库本身,而不是只是内部的DataSet ...如何实现这一点?

  // DataSet1使用到Database.mdb $ b的连接$ b //由创建者

// Users表有3列,id,名称和密码

DataSet1 set = new DataSet1();
UsersTableAdapter adap = new UsersTableAdapter();
DataSet1.UsersRow row = set.Users.AddUsersRow(asd,asd);
int count = adap.Insert(das,das);
MessageBox.Show(row.RowState.ToString()+:+ count.ToString());
count = adaptation.Update(set.Users);
set.AcceptChanges();
MessageBox.Show(row.RowState.ToString()+:+ count.ToString());

//消息框显示:已添加:1
//第二个:不变:1

MessageBox.Show(set.Users。 Rows.Count.ToString());

//返回2...


解决方案

不太了解你的代码,尝试从MSDN中读取这个方法:



http://msdn.microsoft.com/en-us/library/ms233812(v = VS.80) .aspx



更新:与数据集,通常会发生什么(假设数据集已经填充)在代码中添加,编辑或删除。这些行将具有相应的 RowState ,表示它们被添加,编辑,删除或未修改。在 .Update 中,关联的表适配器(具有关联的insert / update / delete命令)将根据行的状态来迭代行并在给定行上执行命令。当行被迭代时,调用 .AcceptChanges ,将行状态恢复为默认值。



如果您调用 AcceptChanges 在更新之前,不会发生任何事情。每行的 RowState 将丢失,因此 .Update 将不具有执行更新所需的必需信息到数据库。如果您有自定义代码将其全部包装在一个事务中,那么您需要确保提交事务。



在您的示例中,您似乎暗示您正在使用表适配器而不是数据集本身。我会建议这样做 - 表适配器通常将方法放在您应该使用的数据集中的表格上。



更新2:代码看起来对我来说,虽然您不需要在适配器上调用插入,或更新后的 AcceptChanges (后者自动完成)。这导致我相信你的插入命令SQL有一个错误 - 尝试提取命令使用的SQL并手动对数据库运行。



更新3:以下代码对我有效:

  static void Main(string [] args)
{
db1DataSet set = new db1DataSet();
set.Users.AddUsersRow(asd,asd);

foreach(set.Users.Rows中的DataRow行)
{
对象foo = row.RowState; //在调试器中确认行状态。
}

UsersTableAdapter adap = new UsersTableAdapter();
adap.Update(set.Users);

Console.Read();
}


I'm having some trouble getting my DataSet to work.

I have a MDB-Database in the background and created a DataSet out of it. Now I created a new method that lets me create a new user in the table.

But when I call it, nothing happens. No exceptions, no errors and I even get "1" returned as number of affected rows. But when I look in the database, no user was added.

I have the feeling that I miss to somehow tell the DataSet that I want to operate the Database itself rather than just the internal DataSet... How can I achieve this?

// DataSet1 uses the connection to the Database.mdb
// Created by the Designer

// The Users table has 3 columns, id, name and password

DataSet1 set = new DataSet1();
UsersTableAdapter adap = new UsersTableAdapter();
DataSet1.UsersRow row = set.Users.AddUsersRow("asd", "asd");
int count = adap.Insert("das", "das");
MessageBox.Show(row.RowState.ToString() + ": " + count.ToString());
count = adap.Update(set.Users);
set.AcceptChanges();
MessageBox.Show(row.RowState.ToString() + ": " + count.ToString());

// The Messagebox shows: "Added: 1"
// The second one: "Unchanged: 1"

MessageBox.Show(set.Users.Rows.Count.ToString());

// Returns "2"...

解决方案

Not knowing too much about your code, try reading this how to from MSDN:

http://msdn.microsoft.com/en-us/library/ms233812(v=VS.80).aspx

Update: with data sets, what would normally happen (assuming the data set has been populated) is rows would be added, edited, or deleted in code. These rows would have a corresponding RowState signifying them as added, edited, deleted, or unmodified. During the .Update the associated table adapter, complete with associated insert/update/delete commands, will iterate the rows and execute the command on a given row depending on the row's state. When the rows have been iterated, .AcceptChanges is called, reverting the row state's to default.

If you call AcceptChanges before updating, then nothing will happen. The RowState of each row will be lost so the .Update will not have the required information it needs to perform the update to the database. If you have custom code for wrapping it all in a transaction, then you need to make sure you commit the transaction.

In your example, you seem to imply you are using the table adapter and not the data set itself. I would advise against this - the table adapters usually place methods on the tables in a data set that you should use.

Update 2: the code looks OK to me, though you don't need to call Insert on the adapter, or AcceptChanges after the update (the latter is done automatically). This leads me to believe there is a bug in your insert command SQL - try extracting the SQL used by the command and run it manually against the database.

Update 3: the following code works fine for me:

static void Main(string[] args)
    {
        db1DataSet set = new db1DataSet();
        set.Users.AddUsersRow("asd", "asd");

        foreach (DataRow row in set.Users.Rows)
        {
            object foo = row.RowState; // Confirm row state in debugger.
        }

        UsersTableAdapter adap = new UsersTableAdapter();
        adap.Update(set.Users);

        Console.Read();
    }

这篇关于插入未提交到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆