473,666 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to Create a Test Class for System.Data.Sql Client

I have been trying to create a class similar to SqlClient but one that
prints sql statements instead of running them on the database. This
way when I want to go into test mode I comment out the SqlClient class
and comment in the test class.

The problem I am facing now is I have to limit myself to simple
functions for the SqlDataReader class. I would like to copy in the
SqlDataReader object exactly the way it is so that I can use this test
code universally on every C# project. The final result would be
calling this sql test class defines SqlDataReader exactly the way it
is in SqlClient, but SqlCommand will be defined differently so that
all sql update/insert/delete commands are printed and never actually
run on the database.

Any ideas on how I might go about this?

Is there a way I can define SqlDataReader within a user defined class
SqlTest and have it extend the SqlDataReader class of SqlClient? Then
I already have my own definition for SqlCommand that works like a
charm for the kind of testing I am doing.

Another possibility can I call SqlClient to get the SqlDataReader
class but then override the SqlCommand class with the functions I
already defined? I need some help with this.

Mark

Feb 27 '07 #1
5 2509
class SqlCommand : DbCommand

You can try to derive your own class from DbCommand and implement its
abstract methods.

Then in your code you can do this:

DbCommand cmd = new SqlCommand();
cmd.MethodX();

When you need to use your class instead, replace the line above with

DbCommand cmd = new YourCommandClas s();
cmd.MethodX();

<mm******@gmail .comwrote in message
news:11******** *************@p 10g2000cwp.goog legroups.com...
>I have been trying to create a class similar to SqlClient but one that
prints sql statements instead of running them on the database. This
way when I want to go into test mode I comment out the SqlClient class
and comment in the test class.

The problem I am facing now is I have to limit myself to simple
functions for the SqlDataReader class. I would like to copy in the
SqlDataReader object exactly the way it is so that I can use this test
code universally on every C# project. The final result would be
calling this sql test class defines SqlDataReader exactly the way it
is in SqlClient, but SqlCommand will be defined differently so that
all sql update/insert/delete commands are printed and never actually
run on the database.

Any ideas on how I might go about this?

Is there a way I can define SqlDataReader within a user defined class
SqlTest and have it extend the SqlDataReader class of SqlClient? Then
I already have my own definition for SqlCommand that works like a
charm for the kind of testing I am doing.

Another possibility can I call SqlClient to get the SqlDataReader
class but then override the SqlCommand class with the functions I
already defined? I need some help with this.

Mark

Feb 27 '07 #2
Sorry, I misunderstood your question. Please disregard my answer...

"Ashot Geodakov" <a_********@hot mail.comwrote in message
news:uL******** ******@TK2MSFTN GP05.phx.gbl...
class SqlCommand : DbCommand

You can try to derive your own class from DbCommand and implement its
abstract methods.

Then in your code you can do this:

DbCommand cmd = new SqlCommand();
cmd.MethodX();

When you need to use your class instead, replace the line above with

DbCommand cmd = new YourCommandClas s();
cmd.MethodX();

<mm******@gmail .comwrote in message
news:11******** *************@p 10g2000cwp.goog legroups.com...
>>I have been trying to create a class similar to SqlClient but one that
prints sql statements instead of running them on the database. This
way when I want to go into test mode I comment out the SqlClient class
and comment in the test class.

The problem I am facing now is I have to limit myself to simple
functions for the SqlDataReader class. I would like to copy in the
SqlDataReade r object exactly the way it is so that I can use this test
code universally on every C# project. The final result would be
calling this sql test class defines SqlDataReader exactly the way it
is in SqlClient, but SqlCommand will be defined differently so that
all sql update/insert/delete commands are printed and never actually
run on the database.

Any ideas on how I might go about this?

Is there a way I can define SqlDataReader within a user defined class
SqlTest and have it extend the SqlDataReader class of SqlClient? Then
I already have my own definition for SqlCommand that works like a
charm for the kind of testing I am doing.

Another possibility can I call SqlClient to get the SqlDataReader
class but then override the SqlCommand class with the functions I
already defined? I need some help with this.

Mark


Feb 27 '07 #3
I am just looking to build a class that I can simply comment out or
back in in place of the SqlClient code without changing any code other
than the using statement. So far my code works for the more basic
SqlClient functions, but for the more complex functions I will need to
extend or implement the sql data reader class so all functions are
accounted for.

Feb 27 '07 #4

I would look at the "Simple Factory" design pattern.
http://www.dofactory.com/Patterns/PatternFactory.aspx
12/1/2005
Understanding the Simple Factory Pattern
http://sholliday.spaces.live.com/blog/
And you can return the actual (real) SqlClient or SqlCommand or SqlReader.

SqlReader implements IDataReader. Thus you could create your own
IDataReader, and have a factory.
Look at the ~interfaces of those real sql objects.


<mm******@gmail .comwrote in message
news:11******** **************@ 8g2000cwh.googl egroups.com...
I am just looking to build a class that I can simply comment out or
back in in place of the SqlClient code without changing any code other
than the using statement. So far my code works for the more basic
SqlClient functions, but for the more complex functions I will need to
extend or implement the sql data reader class so all functions are
accounted for.

Feb 27 '07 #5
And you can return the actual (real) SqlClient or SqlCommand or SqlReader.

SqlReader implements IDataReader. Thus you could create your own
IDataReader, and have a factory.

Look at the ~interfaces of those real sql objects.
I can't return the actual SqlReader or SqlCommand because that would
require me to include the System.Data.Sql Client in the original
function. I want to get away from using the SqlClient in the orignal
function, so that when I debug I comment out SqlClient and comment in
SqlTest. This way it will automatically use the same code but the
code will act differently for debug mode.

Feb 27 '07 #6

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

Similar topics

3
5711
by: Ian | last post by:
The beginning of my assembly that I am getting the access error from looks like this. ********************************* Imports System.EnterpriseServices Imports System Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Data.Common Imports System.Reflection
7
17875
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes); byte key = Encoding.ASCII.GetBytes("0123456789012345");
4
2216
by: J | last post by:
hi, all, I have an asp.net page which let user view data from database (using dataset), but now, how can I create a CSV file in asp.net then let user save to their local machine? Thanks in advance. J.
3
2088
by: Kenneth P | last post by:
Hi, I have a very good book on asp.net Sams ASP.NET Tips, Tutorials and Code ISBN 0-672-32143-2 and is trying to teach myself from examples from this book Chapter 16. Anyway I come quite close when trying to run one example but then I get this message I don't understand: "This .resources file shouldn't be used with this reader. Your resource
1
1409
by: Jim in Arizona | last post by:
I was, for the most part, following the directions at http://aspalliance.com/articleViewer.aspx?aId=138&pId= . I shortened mine a bit so that I only have to click browse, select my image and hit the submit button. I get this error though when I try: Server Error in '/' Application. -------------------------------------------------------------------------------- Input string was not in a correct format.
2
2023
by: whitethomas12 | last post by:
Hi, I currently have some basic code that allows me to run the tracert command through VB.NET and it also updates my database based on the results. I was wondering if someone can help me find a way that I can used my code to run tracert on mutiple instances at the same time. The following is my current code.
1
3372
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons: 1) export to excel button exports the visible columns from the datagridview to excel (this works fine)
8
4432
by: ssmeshack | last post by:
Hai All, Im doing a login page.... And that was without cookie.... I realy dono how to create cookie... pls anyone can help... Here is code from Default.aspx for login control: <asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
1
1471
by: sahashra | last post by:
I am trying to insert values into the table using the values from the form I following is the code I am using protected void Button_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=MyFavs;Integrated Security=SSPI"); string insertstring = @"Insert into MyFavs values (@FirstName,@LastName,@Address,@City,@State,@Phone,@Email,@Password)"; SqlCommand cmd =...
0
8356
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
8871
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
7387
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...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
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
4198
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...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.