473,785 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedded SQL in C#

Hi,
I am intersted in trying to reduce the cost of C# development, by
reducing the number of lines of code. In my opinion, as a business
developer, the biggest opportunity to reduce the number of lines of
code is in database access.

My reality is that I use relational databases a lot. Other constructs,
such as flat files, Web Services, arrays, etc I don't use very much.

My data is stored on relational databases. I think that's pretty
common, nowadays.

I have been using ADO.NET and was wondering about the future.

Will C# support truly embedded SQL in the future? For example, in
PowerBuilder, since 1992, you could code as follows:

Long ll_count
String ls_last_name
SELECT Count(*), max(last_name) INTO :ll_count, :ls_last_name USING
TXN1;

IF TXN1.SQLCODE<>0 THEN ...

In C#, it seems to take many more lines of code to do the same thing.

Further, in PowerBuilder, the above statement works for any relational
database. All you have to do is change the connection parameters.

It seems that in C#, you have to contend with named parameters for SQL
Server and Oracle, vs. positional parameters for ODBC and OLE-DB.
Further, you have to explicitly create every single parameter in C#,
while in the example above, the parameters are created for you, using
datatypes which match the datatypes you use. That is a lot simpler.

Also, in C#, you are forced to use multiple TRY-CATCH constructs after
each SQL statement is executed. And, the exceptions which you catch
are different for different databases. That's an awful lot of code.

PowerBuilder has TRY-CATCH too, but it is generally not used for
relational database access.

And, let's talk cursors. In PowerBuilder, COBOL, and a few other
languages, such as PL*SQL, you can explicitly declare, open, fetch, and
close cursors. Wouldn't it be great if you could do that in C#?

Cursors give you low-level control. They are simple. And, if you
fetch too much data, you can stop.

In PowerBuilder and COBOL, you can even 'fetch in batches'. This means
that instead of getting one row at a time from the database, you
actually grab, say, 1000 rows at a time. Fetching in batches can
improve runtimes by 20 to 1, if your database is over a Wide-Area
Network, or by 4 to 1 if it is on a Local Area Network, in my
experience.

I think Embedded SQL makes sense because maybe, just maybe, it would
not become obsolete, as have a few other technologies (RDO, DAO, ADO,
etc).

Also, I think Embedded SQL is inherently much simpler than all the
above.

For example, a severe performance problem in a classic ASP application
was impacting one of my customers. The ASP app used ADO. I have read
many books about ADO, but not a one of them explained to me how a
"keyset" worked. It turns out that a keyset reads *all* the keys from
your SQL table, and puts them into memory. Well, when your table has 3
million rows, this becomes untenable. But then, how many people know
about this fact?

The reality is that RDO, DAO, ADO and ADO.NET all do things "behind the
scenes" which make performance tuning difficult. You have to capture
the actual SQL they send to the database first, then tune.

I don't want to need ADO to do that for me. Wouldn't you rather just
have real embedded SQL?

Opinions?

VictorReinhart

Dec 29 '05
25 11146
<<simple and elegant expression of queries >>

Declare C1 Cursor for select c.Name,o.OrderI D,o.Amount,c.Ag e from custs
c, orders o where o.Customer = c.Name;
String ls_orderID
Decimal ldc_amt
Long ll_age

Open C1;
DO
Fetch C1 into :ls_orderID, :ldc_amount, :ll_age;
IF sqlca.sqlcode<> 0 THEN EXIT
MessageBox('Row ',ls_orderID + ' ' + String(ldc_amou nt) + ' ' +
String(ll_age))
LOOP WHILE TRUE
Close C1;

Well, let's compare. The LINQ example is 16 lines of code, my example
is 11. That's pretty close, I like the number of lines of code.

But there is one huge difference: I can copy and paste my SQL statement
and paste it into Query Analyzer to unit test it. How does one unit
test the SQL when using LINQ?

Victor Reinhart

Dec 29 '05 #11
There is a big difference between lower- and higher-level languages. If an
app takes days to write in assembly, then it might take hours to write in
c#.

Sometimes the extra effort put upfront can pay off. This is something that
is often overlooked. Sometimes architectures do require extra effort and
testing, but when done, can easily be consumed by other parts of the code,
and when boiler-plates are tested, then anything consuming them can have a
high degree of confidence rather than writing the same or similar code each
time it is needed.

I may not know your exact circumstances, but I do not see how saving a few
minutes by writing one line to talk to the database rather than a few lines
is going to affect the budget. But then that is just me.

Your main thread did not mention that you were using a website. But, since
you mentioned it, you can write a website with no physical pages and one
class (or a few classes). You could write something like an HttpHandler or
HttpModule (can't remember which) that intercepts the page requests. Rather
then letting ASP.NET handler the request, you could encapsulate all the
behavior and rendering within the class and just stream the content to the
browser. Therefore you could create all 200 pages with only a single class
to handler the requests. No code-behinds or other utilities.
"VictorReinhart " <vi************ **@phs.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
<<How do you equate development costs with the number of lines of
code?>>
There isn't an exact correlation. However, there is a gross
correlation. That is why we use high-level languages instead of
low-level languages, such as assembler. In fact, in my opinion, it is
best to use the highest level language possible as much as possible,
then only "drop-down" to other languages when absolutely necessary.

<<Are your developers getting paid by the line? >>
Of course not. But, we want to create the most value we can in a given
amount of time. It takes more time to write boiler-plate code than to
not write it. It takes more time to debug boiler-plate code. It takes
more time to read boiler-plate code. When the code is concise, time
and money are saved.

<<There is more than just the initial time to develop an app.>>
That is true, but time wasted is money wasted. The reality is that
money wasted developing an application can kill it before it is
completed. This is a common problem.

<<For instance, it is fully possible to write a large app with only one
class>>
I don't understand. I have about 200 web application pages to create.
How is that done using only one class?

Dec 29 '05 #12
<<Sometimes the extra effort put upfront can pay off.>>
Maybe, but in my experience, the extra effort usually blows the budget.
My focus is to not spend that effort if possible. That's why embedded
SQL makes sense.

<<I do not see how saving a few minutes by writing one line to talk to
the database rather than a few lines is going to affect the budget. >>
Let me explain. If a SQL statement has only two tables and 3 columns,
it might be pretty close using LINQ or Embedded SQL. But more often,
it is a 3 or 4 table join, and/or 6 to 12 columns. Now, if that SQL
statement is failing, do you see the benefit of using copy and paste?
You could copy the query from your source code, then paste it into
SQL*Plus or Query Manager, then test the SQL. Time is money. How do
you do this using LINQ? How could you know what SQL a complex query in
LINQ generates?

<<you can write a website with no physical pages and one class (or a
few classes).>>
How does one do this, and also lay out the design so that you can
visually see how the pages will look? Visual layout is very important
to improve productivity.

Dec 29 '05 #13
<<I may not know your exact circumstances, but I do not see how saving
a few
minutes by writing one line to talk to the database rather than a few
lines
is going to affect the budget. But then that is just me.>>

My application has about 250 tables. That's not unusual. I have
worked on numerous business applications with 250 to 400 tables or
more. With that many tables, there is going to be a lot of SQL to
insert, delete, update and select from all those tables.

That is very, very common.

So, there is going to be a lot of C# code declaring parameters, etc.

Further, these tables are frequently joined. Frequently, these are
non-trivial joins, with 3 to 6 tables or even more. Sometimes, we even
join views. Very often, we use a combination of outer joins with inner
joins. Very often, there are bugs in these queries. Also, these
queries tend to require maintenance -- adding colums, adding tables,
changing the WHERE clause.

In my applications, without exception, the SQL interface is hugely
important.

"A few lines" turns into "quite a few lines per query" times hundreds
or thousands of queries.

That affects the budget big-time.

Victor Reinhart

Dec 29 '05 #14
You might want to try the Data Access Application Block from Microsoft.
This wraps many of the common code functionality.

http://msdn.microsoft.com/library/de.../html/daab.asp
"VictorReinhart " <vi************ **@phs.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
<<I may not know your exact circumstances, but I do not see how saving
a few
minutes by writing one line to talk to the database rather than a few
lines
is going to affect the budget. But then that is just me.>>

My application has about 250 tables. That's not unusual. I have
worked on numerous business applications with 250 to 400 tables or
more. With that many tables, there is going to be a lot of SQL to
insert, delete, update and select from all those tables.

That is very, very common.

So, there is going to be a lot of C# code declaring parameters, etc.

Further, these tables are frequently joined. Frequently, these are
non-trivial joins, with 3 to 6 tables or even more. Sometimes, we even
join views. Very often, we use a combination of outer joins with inner
joins. Very often, there are bugs in these queries. Also, these
queries tend to require maintenance -- adding colums, adding tables,
changing the WHERE clause.

In my applications, without exception, the SQL interface is hugely
important.

"A few lines" turns into "quite a few lines per query" times hundreds
or thousands of queries.

That affects the budget big-time.

Victor Reinhart

Dec 29 '05 #15

"VictorReinhart " <vi************ **@phs.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
<<I may not know your exact circumstances, but I do not see how saving
a few
minutes by writing one line to talk to the database rather than a few
lines
is going to affect the budget. But then that is just me.>>

My application has about 250 tables. That's not unusual. I have
worked on numerous business applications with 250 to 400 tables or
more. With that many tables, there is going to be a lot of SQL to
insert, delete, update and select from all those tables.

That is very, very common.
Yes it is.

So, there is going to be a lot of C# code declaring parameters, etc.
Which Visual Studio or any number of tools can automate for you.

Further, these tables are frequently joined. Frequently, these are
non-trivial joins, with 3 to 6 tables or even more. Sometimes, we even
join views. Very often, we use a combination of outer joins with inner
joins. Very often, there are bugs in these queries. Also, these
queries tend to require maintenance -- adding colums, adding tables,
changing the WHERE clause.


Which is one of the reasons embedding non-trivial SQL in applciation code,
whether using dynamic SQL or static embedded SQL is a bad idea. Non-trivial
SQL statements belong in the database, not the application. An application
should be coded against a simple service layer in the database consisting of
single table and view access and stored procedures. If you need to join
multiple tables and do non-trivial SQL, it's much easier to code, debug,
maintain and port it at the database tier.

I'm pretty liberal about allowing SQL in an application. My simple rule for
SQL in the application tier is simple: no joins. If you need a join, code
it in a view, procedure, UDF, etc and call it from client code. The need
for client code to join indicates that your data model is exposed with too
much granularity, and your application has too much knoledge about the
details of your relational schema design.

Embedding SQL in the application simply violates the seperation of tiers,
technologies and developer skill-sets which is the foundation of enterprise
application architecture. It works for simple applications, but in
substantial enterprise applications with lots of tables, lots of business
rules, lots of technical roles, etc, it just won't do.

David
Dec 29 '05 #16
b. is not true. You can either handle specific exceptions or
trap a generic one which will still return the problem in
the database.

c. I think you'll find that most architects take a much
different design route than you do in regards
to the database access layer, stored procedures,
and proprietary features of the database you are after.

There are all sorts of database factory patterns
that would enable multiple database support.

I think you are trying to argue a point about embedded
or dynamic sql that most high end developers have largely
abandoned. They tend to build systems that will scale
well and perform well. Many use code generators
to write the code for them so they can optimize
speed to the database and work in OOP designs.

Whether DLinq catches on or not remains to be seen.

--
Robbe Morris - 2004/2005 Microsoft MVP C#
http://www.eggheadcafe.com/forums/merit.asp

"VictorReinhart " <vi************ **@phs.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
<<DLinq in C# 3.0 is probably what you are after. >>

I read the MSDN article: "The LINQ Project", September 2005. Thank
you for the suggestion, but It is not what I'm after. While the intent
is noble, I would rather have embedded SQL, for all the reasons above.
But if that was not enough, here are more:

a) Lack of Control over the SQL
What SQL does DLinq generate?
How to tell?
My experience is that when tuning problems happen, you have to see
and modify the SQL. For example, in SQL Server, you might need to add
the "nolock" keyword. How does one do this using DLinq? For Oracle,
how would one create a table which is like another table, and specify a
tablespace? Example:
create table hed_prov_az tablespace zhedhead
as select * from hed_prov_addres s where 1=2;
I don't know about your DBA's, but mine require me to specify a
tablespace when I create a table.

b) TRY-CATCH.
I rarely see anything in dot net which is a full example, with a
real TRY-CATCH. The way the code is written in the examples, your
application will crash with the first exception.

c) Which databases does it or will it support?
Embedded SQL should work for all databases.

d) It would be helpful to include examples using NULL Values.
Null values are common for things like termination dates. Yet, you
rarely see any examples.

<<most applications make use of stored procedures versus sql>>
I don't know of very many which use sp's. And sp's are a bad fit for
the application which targets multiple databases. We would rather use
SQL, thank you very much, instead of writing the same stored procedures
in three languages.

<<You wind up sending less across the network when making database
calls. >>
Maybe a trivial savings there, but development costs are a lot higher.

The reality is, SQL is here to stay, and is extremely useful.
Sometimes, you have to add hints to make it work, sometimes you need
database-specific code. But, in my opinion, what we really don't need
is software which hides the SQL.

I vote for embedded SQL.

VictorReinhart

Dec 29 '05 #17
Hi,

VictorReinhart wrote:
if that SQL statement is failing, do you see
the benefit of using copy and paste?


Just as a side note -- this is one of the significant advantages of using
stored procedures over embedded SQL statements: stored procedures are
validated at the time of creation or change.

Another advantage is some degree of encapsulation of the database innards.
Personally, I'd hate to be the one to hunt down and fix who-knows-how-many
embedded SQL statements in a large project (the size of those to which you
refer in another post), should there ever be a need to alter the database
design.

--
Chris Priede
Dec 29 '05 #18
Thank you for the article. It is interesting, but it does not target
all the databases I use. Also, the article says it does not yet
support .NET Framework 2.0. I guess my concern, too, is that after
investing time and money in one of these factories, who knows if it
will ever be upgraded for the future?

Victor Reinhart

Peter Rilling wrote:
You might want to try the Data Access Application Block from Microsoft.
This wraps many of the common code functionality.

http://msdn.microsoft.com/library/de.../html/daab.asp
"VictorReinhart " <vi************ **@phs.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
<<I may not know your exact circumstances, but I do not see how saving
a few
minutes by writing one line to talk to the database rather than a few
lines
is going to affect the budget. But then that is just me.>>

My application has about 250 tables. That's not unusual. I have
worked on numerous business applications with 250 to 400 tables or
more. With that many tables, there is going to be a lot of SQL to
insert, delete, update and select from all those tables.

That is very, very common.

So, there is going to be a lot of C# code declaring parameters, etc.

Further, these tables are frequently joined. Frequently, these are
non-trivial joins, with 3 to 6 tables or even more. Sometimes, we even
join views. Very often, we use a combination of outer joins with inner
joins. Very often, there are bugs in these queries. Also, these
queries tend to require maintenance -- adding colums, adding tables,
changing the WHERE clause.

In my applications, without exception, the SQL interface is hugely
important.

"A few lines" turns into "quite a few lines per query" times hundreds
or thousands of queries.

That affects the budget big-time.

Victor Reinhart


Dec 30 '05 #19
VictorReinhart <vi************ **@phs.com> wrote:
I am intersted in trying to reduce the cost of C# development, by
reducing the number of lines of code. In my opinion, as a business
developer, the biggest opportunity to reduce the number of lines of
code is in database access.


<snip>

You might want to look into NHibernate:
http://www.nhibernate.org

I don't have any experience with NHibernate itself, but Hibernate (its
Java equivalent) is excellent, and has taken a lot of the work of data
access away in the project I've just been involved in.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4472
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
11
4270
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
8
8091
by: Colleyville Alan | last post by:
I have been working on an Access app that takes info from a file and writes it to a spreadsheet on a form, simultaneously saving the spreadsheet to Excel. I got the idea that the same concept could work in reverse, i.e. we have a cost model written in Excel that calculates the profitability of customer accounts based on several inputs and they need to be updated at least once per year. These cost models sit on lots of people's hard...
0
2223
by: Nick White [MSFT] | last post by:
Hello fellow Microsoft Windows Mobile and Embedded enthusiasts: I invite you to peruse the list below of upcoming technical chats and Webcasts offered by the Windows Mobile and Embedded Devices Group. For the full list of upcoming Windows Mobile and Embedded chats, to review archived chat sessions, or to request a reminder for a chat, visit http://msdn.microsoft.com/embedded/community/community/chats/. Also, the Windows Mobile and...
59
7221
by: Jeff Bowden | last post by:
For ease of configuration and other reasons, I would like for my single-user GUI app to be able to use postgresql in-process as a library accessing a database created in the users home directory. I think I could possibly get what I want by launching a captive copy of postmaster with appropriate args but it seems conceptually cleaner to not have a seperate process at all. Has anyone tried to do anything like this? I've looked at sqlite...
49
8974
by: Alex Vinokur | last post by:
Are there any restrictions/problems for use of C++ STL in development in embedded systems? In particular: * Does STL require too much space/memory? * Is 'implementation of STL algorithms/methods' reenterable/reentrant? * What is the cost to provide continuity of vectors in memory? Any other problems? -- Alex Vinokur
0
2271
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard requirement of most applications. But users are increasingly demanding more sophisticated reporting from applications - seeking such features as custom report design, ad hoc report creation and analytics. Developers that want to embed business...
1
1654
by: leeanngriego | last post by:
I have a client who has asked me to find him some solid up and coming embedded engineers. 2 to 3 years expereince with Embedded Linux, VxWorks, Nucleus or any other RTOS who has working in L2/L3 networking. The object is to bring them on board and train in SAN/NAS switching, as long as they are solid C, Unix and Embedded programmers that have the work ethic and talent to be very good, he will train whatever else is necessary. See...
20
2054
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
There are a few guarantees I exploit in the C Standard. For instance, I might write (unsigned)-1 to get the maximum value for an unsigned integer. Also, I might rely on things such as: memset(data,-1,sizeof data)
30
4302
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.