473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

inline SQL to stored procedure

I'm going about converting all of my inline SQL in my C# application
into stored procedures. I have this method below that is running an SQL
statement until a field changes. Is it a good idea to convert this to a
stored procedure, and if so should it be easy to do?

DBResult dbrChangeCCState;
string strViewAttemptedPurchase;

strViewAttemptedPurchase = "SELECT STATUS FROM ATTEMPTEDPURCHASE
WHERE SESSIONID = " + Convert.ToInt64(strSessionID) + " AND PURCHASEID =
" + Convert.ToInt64(strPurchaseID);

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
//SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
SqlCommand objCommand = new SqlCommand(strViewAttemptedPurchase,
objConnection);

try
{
objConnection.Open();
SqlDataReader objDataReader = null;
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

DateTime dtmStart = DateTime.Now;
TimeSpan tsTimeout = new TimeSpan (0, 1, 0); // One minute

while ((intNewStatus == 0) || (intNewStatus == 1))
{
objDataReader.Close();
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

if (DateTime.Now-dtmStart > tsTimeout)
{
break;
}
}

dbrChangeCCState = DBResult.Valid;
}
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 7042
Mike,

It would be incredibly easy, it would seem. You would just need two
parameters, one for the session id and one for the purchase id. They would
be of type bigint. Other than that, you wouldn't have to do much to change
your code.

Also, you dont have to do this:

intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);

Rather, you can do this:

intNewStatus = (int) objDataReader["STATUS"];

Assuming that the STATUS field is of type int. It's not necessary, and
makes for more work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm going about converting all of my inline SQL in my C# application
into stored procedures. I have this method below that is running an SQL
statement until a field changes. Is it a good idea to convert this to a
stored procedure, and if so should it be easy to do?

DBResult dbrChangeCCState;
string strViewAttemptedPurchase;

strViewAttemptedPurchase = "SELECT STATUS FROM ATTEMPTEDPURCHASE
WHERE SESSIONID = " + Convert.ToInt64(strSessionID) + " AND PURCHASEID =
" + Convert.ToInt64(strPurchaseID);

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
//SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
SqlCommand objCommand = new SqlCommand(strViewAttemptedPurchase,
objConnection);

try
{
objConnection.Open();
SqlDataReader objDataReader = null;
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

DateTime dtmStart = DateTime.Now;
TimeSpan tsTimeout = new TimeSpan (0, 1, 0); // One minute

while ((intNewStatus == 0) || (intNewStatus == 1))
{
objDataReader.Close();
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

if (DateTime.Now-dtmStart > tsTimeout)
{
break;
}
}

dbrChangeCCState = DBResult.Valid;
}
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Write the whole lot in SQL ... you don't look like you've got any code that
updates UI there... could the whole lot be done as one SP, including the
loop..?

"Mike P" wrote:
I'm going about converting all of my inline SQL in my C# application
into stored procedures. I have this method below that is running an SQL
statement until a field changes. Is it a good idea to convert this to a
stored procedure, and if so should it be easy to do?

DBResult dbrChangeCCState;
string strViewAttemptedPurchase;

strViewAttemptedPurchase = "SELECT STATUS FROM ATTEMPTEDPURCHASE
WHERE SESSIONID = " + Convert.ToInt64(strSessionID) + " AND PURCHASEID =
" + Convert.ToInt64(strPurchaseID);

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
//SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
SqlCommand objCommand = new SqlCommand(strViewAttemptedPurchase,
objConnection);

try
{
objConnection.Open();
SqlDataReader objDataReader = null;
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

DateTime dtmStart = DateTime.Now;
TimeSpan tsTimeout = new TimeSpan (0, 1, 0); // One minute

while ((intNewStatus == 0) || (intNewStatus == 1))
{
objDataReader.Close();
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

if (DateTime.Now-dtmStart > tsTimeout)
{
break;
}
}

dbrChangeCCState = DBResult.Valid;
}
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Actually, writing the whole thing as a stored proc turned out easier
than I thought (and I learned a couple things about SQL along the way....)

Create PROCEDURE TestStatus
@SessionID bigint,
@PurchaseID bigint
AS

SET NOCOUNT ON

DECLARE @dtStop datetime
DECLARE @intNewStatus integer

Set @intNewStatus = 0 --- initialize to a "keep looping value"
SET @dtStop = DateAdd("mi", 1, GetDate()) -- set to one minute from now.

while (@dtStop > GetDate()) and (@intNewStatus = 0 or @intNewStatus =1)
begin

SELECT @intNewStatus = STATUS FROM ATTEMPTEDPURCHASE
WHERE SESSIONID = @SessionID AND PURCHASEID = @PurchaseID

waitfor delay '00:00:01'
end

return @intNewStatus
GO
The "waitfor delay" line isn't necessary, but it limits the select to being
run only once a second during the minute you are waiting for the status to
change. Otherwise it will query the table several thousand times during
that minute , (1,600,000+ times on my 2.6Ghz P4) while using all available
CPU cycles.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm going about converting all of my inline SQL in my C# application
into stored procedures. I have this method below that is running an SQL
statement until a field changes. Is it a good idea to convert this to a
stored procedure, and if so should it be easy to do?

DBResult dbrChangeCCState;
string strViewAttemptedPurchase;

strViewAttemptedPurchase = "SELECT STATUS FROM ATTEMPTEDPURCHASE
WHERE SESSIONID = " + Convert.ToInt64(strSessionID) + " AND PURCHASEID =
" + Convert.ToInt64(strPurchaseID);

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
//SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"
]);
SqlCommand objCommand = new SqlCommand(strViewAttemptedPurchase,
objConnection);

try
{
objConnection.Open();
SqlDataReader objDataReader = null;
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

DateTime dtmStart = DateTime.Now;
TimeSpan tsTimeout = new TimeSpan (0, 1, 0); // One minute

while ((intNewStatus == 0) || (intNewStatus == 1))
{
objDataReader.Close();
objDataReader = objCommand.ExecuteReader();

if (objDataReader.Read() == true)
{
intNewStatus = Convert.ToInt32(objDataReader["STATUS"]);
}

if (DateTime.Now-dtmStart > tsTimeout)
{
break;
}
}

dbrChangeCCState = DBResult.Valid;
}
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4
Better to have used parameters in the first place (see below). Once you've
done that it's easy to turn it in to an SP. Just put the SQL in the SP and
change the command string to the name of the SP, and set the CommandType to
StoredProcedure.

Since you're just returning one value, I also changed the code to
ExecuteScalar(), which gets rid of a lot of code. If you stay with the
DataReader you can also consider:

intNewStatus = (int)objDataReader["STATUS"];
// or
intNewStatus = objDataReader.GetInt32(0);

--Bob

DBResult dbrChangeCCState;
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectMcCallumTest"]);
SqlCommand objCommand = new SqlCommand("SELECT Status FROM AttemptedPurchase
WHERE SessionID = @SessionID AND PurchaseID = @PurchaseID",objConnection);

// Following assumes the fields are really BigInts.

objCommand.Parameters.Add("@SessionId",SqlDbType.B igInt).Value =
Convert.ToInt64(strSessionID);
objCommand.Parameters.Add("@PurchaseId",SqlDbType. BigInt).Value =
Convert.ToInt64(strPurchaseID);

try
{
objConnection.Open();
intNewStatus = (int)objCommand.ExecuteScalar();
DateTime dtmStart = DateTime.Now;
TimeSpan tsTimeout = new TimeSpan (0, 1, 0); // One minute

while ((intNewStatus == 0) || (intNewStatus == 1))
{
intNewStatus = (int)objCommand.ExecuteScalar();

if (DateTime.Now-dtmStart > tsTimeout)
{
break;
}

// where's your catch and/or finally block??
}

dbrChangeCCState = DBResult.Valid;
}
Nov 16 '05 #5

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

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
4
by: Wendy Elizabeth | last post by:
I have the following questions about VB.NET interfacing with sql server 2000: 1. I have heard that VB.NET can run with inline SQL. Can you show me how to use inline sql to access a sql server 2000...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
1
by: kentk | last post by:
Is there a difference in how SQL Server 7 and SQL 2000 processes SQL passed from a program by an ADO command object. Reason I ask is I rewrote a couple applications a couple years ago were the SQL...
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
15
by: Burt | last post by:
I'm a stored proc guy, but a lot of people at my company use inline sql in their apps, sometimes putting the sql in a text file, sometimes hardcoding it. They don't see much benefit from procs, and...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.