Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
public abstract IDbDataAdapter GetAdapter(string sql);


And the derived class cDbOle override that method as follow:

C#
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;
	}


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)
Posted
Updated 12-Mar-19 0:51am
Comments
[no name] 11-Mar-19 10:34am    
Can't see how _trans is defined. "Boxing" references looks fishy. Can't see why you don't use an "interface" instead of abstract methods. etc.
#realJSOP 11-Mar-19 11:34am    
Interface vs abstract - potay-toe, potah-toe in most cases.

Interfaces can't have implemented methods, so if you need that, abstract is the go-to mechanism. On the other hand, you can only inherit from a single class, but you can inherit multiple interfaces.

I mostly use interfaces when I want to include otherwise dissimilar objects in a single collection of complex types.

I use static classes for my DAL functionality, because the connections are transient. That way, I don't have to inherit from a base class (saving that for really important stuff).
Afzaal Ahmad Zeeshan 11-Mar-19 11:47am    
Interfaces can't have implemented methods,

That is debatable and depends on the language version that you are using.

C# 8 supports default interfaces, that can have a definition for the methods and can do the same thing as the abstract types, and also allow the type to implement other interfaces. Abstract types won't allow that to happen.
#realJSOP 11-Mar-19 13:45pm    
We've already discussed default methods in interfaces here at CP, and have deemed them to be generally pointless. In point of fact, we also decided that MS would have been better served changing some more icons rather than implementing default methods in interfaces.
Afzaal Ahmad Zeeshan 11-Mar-19 14:31pm    
With all the respect, due, that does not take away the argument that interfaces can be implemented. :-)

I found the problem. It was up to the use of "using" on OleDbCommandBuilder. So I changed my code as follows
C#
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;
and the property InserCommand of my da object now is good, no more null!
Sincerely I dunno why
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900