Fill DataSet with nothing
-
Hi, I am reviewing existing code and stumbled across something that, to me - and I'm new to ADO.NET, seems very awkward. A DataSet is filled using a OleDbDataAdapter. The weird thing is that the command does not affect any records at all, intentionally. It looks something like "SELECT ... WHERE 0 = 1". The comment describing the method containing this code says "Retrieve an empty dataset that contains the database structure". DataSet Quick Whatch reports the dataset to be empty after the fill. Does this SELECT statement really do any good? :confused: Thanks for comments, ideas or advise!
-
Hi, I am reviewing existing code and stumbled across something that, to me - and I'm new to ADO.NET, seems very awkward. A DataSet is filled using a OleDbDataAdapter. The weird thing is that the command does not affect any records at all, intentionally. It looks something like "SELECT ... WHERE 0 = 1". The comment describing the method containing this code says "Retrieve an empty dataset that contains the database structure". DataSet Quick Whatch reports the dataset to be empty after the fill. Does this SELECT statement really do any good? :confused: Thanks for comments, ideas or advise!
I don't use datasets because they tend to encourage the data layer to get too close to the presentation layer (not good design). The SELECT statement returns, as you've already noticed, an empty set. But that isn't to say that nothing at all was returned. I'm guessing that the empty DataSet is subsequently used to look at the data schema without actually needing to get any data. Obviously, it would be wasteful to get data that isn't going to be used.
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Hi, I am reviewing existing code and stumbled across something that, to me - and I'm new to ADO.NET, seems very awkward. A DataSet is filled using a OleDbDataAdapter. The weird thing is that the command does not affect any records at all, intentionally. It looks something like "SELECT ... WHERE 0 = 1". The comment describing the method containing this code says "Retrieve an empty dataset that contains the database structure". DataSet Quick Whatch reports the dataset to be empty after the fill. Does this SELECT statement really do any good? :confused: Thanks for comments, ideas or advise!
This is usually a way to get an empty dataset with the correct stucture. It may be used for collecting and adding new rows to the database or perhaps merging data from another program layer before updating the database. I sometimes use this in large stored procedures which produce mutitable datasets; some of the tables may be empty, but my data layer expects the table structure to be there so I can add relationships, so I use a WHERE 0 = 1 to create an empty table.
-
This is usually a way to get an empty dataset with the correct stucture. It may be used for collecting and adding new rows to the database or perhaps merging data from another program layer before updating the database. I sometimes use this in large stored procedures which produce mutitable datasets; some of the tables may be empty, but my data layer expects the table structure to be there so I can add relationships, so I use a WHERE 0 = 1 to create an empty table.
Thank you for your informative answer! However, let's extend my question somewhat. I understand why someone would want to do this, and when to use it. What I don't understand is why this is done in light of what the code does next. instanceDS = m_InstDb.GetInstanceDBDefinition(); string[] arl = new string[1]; arl[0]="T_Project"; instanceDS = m_InstDb.GetDataSetFromTables(arl); Doesn't the fourth line of code overwrite what was initially in 'instanceDS'? If it does, then the first line should be superflous, or am I wrong? Does preparing 'instanceDS' do any good when its about to be assigned something else? The first assignment to 'instanceDS' takes a long time (according a profiling tool I use), because the DataSet is "filled" with the structure of several tables, and my task as of now is to optimize the code for speed. Grateful for more help!
-
Thank you for your informative answer! However, let's extend my question somewhat. I understand why someone would want to do this, and when to use it. What I don't understand is why this is done in light of what the code does next. instanceDS = m_InstDb.GetInstanceDBDefinition(); string[] arl = new string[1]; arl[0]="T_Project"; instanceDS = m_InstDb.GetDataSetFromTables(arl); Doesn't the fourth line of code overwrite what was initially in 'instanceDS'? If it does, then the first line should be superflous, or am I wrong? Does preparing 'instanceDS' do any good when its about to be assigned something else? The first assignment to 'instanceDS' takes a long time (according a profiling tool I use), because the DataSet is "filled" with the structure of several tables, and my task as of now is to optimize the code for speed. Grateful for more help!
Yes, that's not great code. In the most optomistic light, it looks like it might have been introduced during modification of the code (where it might have been used before the re-assignment). In the worst case, I would check if the variable instanceDS has a scope wider than the local procedure and make sure it is not used from within GetDataSetFromTables(). That would be REALLY bad. Also check that no other objects are affected from within GetInstanceDBDefinition(); it might be modifying some properties for later use(side effects).