本文介绍了Idataadapter和insertcommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,它定义了几个方法来处理数据库连接。我还有几个使用它的类,例如一个用于SQL CE,另一个用于通用的OleDb连接类型。

因此,通过抽象类定义以下方法:



Hi, I've an abstract class which define several methods in order to handle db connetions. I've also a couple of class which use it, for example one for SQL CE and another one for generic OleDb connection type.
So, by abstract class define the following method:

public abstract IDbDataAdapter GetAdapter(string sql);





派生类cDbOle覆盖方法如下:





And the derived class cDbOle override that method as follow:

public override IDbDataAdapter GetAdapter(string sql)
	{
		if (!ConnectionOpen())
			return null;

		OleDbCommand cmd = new OleDbCommand(sql, (OleDbConnection)Cnn);

		cmd.CommandType = CommandType.Text;
		if (_trans != null)
			cmd.Transaction = (OleDbTransaction)_trans;
		OleDbDataAdapter da = new OleDbDataAdapter(cmd);
		using (OleDbCommandBuilder cb = new OleDbCommandBuilder(da))
		{
			cb.QuotePrefix = "[";
			cb.QuoteSuffix = "]";
			da.InsertCommand = cb.GetInsertCommand();
			da.DeleteCommand = cb.GetDeleteCommand();
			da.UpdateCommand = cb.GetUpdateCommand();
		}

		return da;
	}





除了InsertCommand和UpdateCommand之外,它基本上工作正常。事实上,在我返回它(并将其转换为IDbDataAdapter)之前,InsertCommand很好,之后(return da)命令为null。 UpdateCommand也一样。

我觉得我做的事情不错,但我不知道如何解决这个错误。



什么我试过了:



Cast



Basically it works fine except for InsertCommand and UpdateCommand. Infact, until I return it (and also cast it to IDbDataAdapter) the InsertCommand is fine, after that ("return da") the command is null. Same for UpdateCommand.
I think I do things correctly but I don't know how to solve this error.

What I have tried:

Cast

OleDbDataAdapter da = new OleDbDataAdapter(cmd)

as

IDbDataAdapter da = new OleDbDataAdapter(cmd)

推荐答案


OleDbDataAdapter da = new OleDbDataAdapter(sql, (OleDbConnection) Cnn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
cb.QuotePrefix = "[";
cb.QuoteSuffix = "]";
da.InsertCommand = cb.GetInsertCommand();
da.DeleteCommand = cb.GetDeleteCommand();
da.UpdateCommand = cb.GetUpdateCommand();

return da;

我的 da 对象的属性InserCommand现在很好,不再为null!

真诚地我不知道为什么

and the property InserCommand of my da object now is good, no more null!
Sincerely I dunno why


这篇关于Idataadapter和insertcommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:28