473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Running Stored Procedure in a Loop

I'm trying to execute a stored procedure in a loop while paging through
database table records but the stored procedure isn't running. I get
the folowing error: The component 'adodb.connection' cannot be created.
Apartment threaded components can only be created on pages with an <%@
Page aspcompat=true %> page directive.

Can anyone tell me what I'm doing wrong? Below is my code.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string
Dim connString As String
Dim strID As String
Dim strSP As String
connString = "Provider=SQLOLEDB;Data Source=OET-JBEACH;Initial
Catalog=TEST;User Id=test;Password=test;"

'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()

Dim strSQL As String = "SELECT Blob_ID, Blob_Date FROM
dbo.tblBlob WHERE Blob_PhotoYesNo=33 ORDER BY Blob_Date desc"

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)

'run stored procedure for each record in the table
While objDataReader.Read()
'set file name
strID = objDataReader("Blob_ID") & "_" &
Right(objDataReader("Blob_Date"), 4) & ".jpg"
'set stored procedure execute line
strSP = "ntext2file, 'c:\blob\" & strID & "', 'tblBlob',
'Blob_Photo', 'where Blob_ID=" & objDataReader("Blob_ID") & "'"

' ***** the response.write part works. the rest doesn't

Response.Write(strSP & "<br>")

Dim c
c = Server.CreateObject("adodb.connection")
c.Open(Application("connString"))
c.Execute(strSP)
c.Close()
c = Nothing
End While

'Close the datareader/db connection
objDataReader.Close()
End Sub
</script>
--
Jerry

May 1 '06 #1
7 3235
Why are you trying to call the SP through an ADODB (COM) object? Why
not use an OleDbCommand? In fact, if you're using Sql Server (which I'm
guessing you are), why not ditch the OleDbConnection/Command for a
SqlConnection/Command, which are optimized for Sql Server...

'***** snip
Dim connection As SqlConnection
Dim command As SqlCommand

connection.Open( Application( "connString" ) )
command = new SqlCommand()
command.CommandType = CommandType.StoreProcedure
command.CommandText = "ntext2file"

' add the parameters...
etc...

'**** end snip...

Sorry, gotta run or I'd finish that code, but it should get you on the
right track...

May 1 '06 #2
Thanks Jason,

I'm very new at .Net. thanks for the start

--
Jerry

May 1 '06 #3
What exactly DOES the response.write show?

It looks like "ntext2file, 'c..."

Which would put a comma right before the first parameter, which is invalid
syntax.

And you should always try to avoid putting a sproc call in a loop like that.
You can certainly do what you want to do in a single call even if you have
to use SQL cursors, but even then, there are ways around that, too

Jef

Jeff
"Jerry" <je*******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I'm trying to execute a stored procedure in a loop while paging through
database table records but the stored procedure isn't running. I get
the folowing error: The component 'adodb.connection' cannot be created.
Apartment threaded components can only be created on pages with an <%@
Page aspcompat=true %> page directive.

Can anyone tell me what I'm doing wrong? Below is my code.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Create a connection string
Dim connString As String
Dim strID As String
Dim strSP As String
connString = "Provider=SQLOLEDB;Data Source=OET-JBEACH;Initial
Catalog=TEST;User Id=test;Password=test;"

'Open a connection
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open()

Dim strSQL As String = "SELECT Blob_ID, Blob_Date FROM
dbo.tblBlob WHERE Blob_PhotoYesNo=33 ORDER BY Blob_Date desc"

'Create a command object
Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(strSQL, objConnection)

'Get a datareader
Dim objDataReader As OleDbDataReader
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConn ection)

'run stored procedure for each record in the table
While objDataReader.Read()
'set file name
strID = objDataReader("Blob_ID") & "_" &
Right(objDataReader("Blob_Date"), 4) & ".jpg"
'set stored procedure execute line
strSP = "ntext2file, 'c:\blob\" & strID & "', 'tblBlob',
'Blob_Photo', 'where Blob_ID=" & objDataReader("Blob_ID") & "'"

' ***** the response.write part works. the rest doesn't

Response.Write(strSP & "<br>")

Dim c
c = Server.CreateObject("adodb.connection")
c.Open(Application("connString"))
c.Execute(strSP)
c.Close()
c = Nothing
End While

'Close the datareader/db connection
objDataReader.Close()
End Sub
</script>
--
Jerry

May 1 '06 #4
I caught that extra comma after I posted. I removed it and tried again
with no luck. The resuts of the response.write are:
ntext2file 'c:\blob\999_2006', 'tblBlob', 'Blob_Photo', 'where Blob_ID
= 999'

I'm exporting images from an ntext field to jpg files. There are over
200 of them so I'm trying to automate it in the loop.

--
Jerry

May 2 '06 #5
You are calling a sproc 200 times in a loop??? Wrong! Like I said, do it in
ONE call. If you need help to understand this, you're in the right group.

Does the query work ONCE in Query Analzyer?

And you are passing in a "where" clause as a parameter? You logic is
incorrect.

Jeff
"Jerry" <je*******@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I caught that extra comma after I posted. I removed it and tried again
with no luck. The resuts of the response.write are:
ntext2file 'c:\blob\999_2006', 'tblBlob', 'Blob_Photo', 'where Blob_ID
= 999'

I'm exporting images from an ntext field to jpg files. There are over
200 of them so I'm trying to automate it in the loop.

--
Jerry

May 2 '06 #6
The query does work in the analyzer. The sp takes the following
parameters:
"EXEC saveNtext2file FileName, tableName, columnName, WhereCondition".
The sp uses the parameters to build the SQL statement: "set @sql =
N'SELECT @c =(select DATALENGTH('+@column+')/2 from '+@table+'
'+@where+')'"

What I ended up doing is exporting all of the Blob_IDs and putting them
into an array. Then using a for loop I step through the array and call
the sp each time.

I'm more used to classic asp but I'll be doing more and more .net work
in the near future. Do you have any suggstions on where I can go to get
some good resources for learning .net (online, books, etc.)?

Thanks,

--
Jerry

May 2 '06 #7
Why export the blob id's into an array. This is what I mean by doing it in a
single call. Do the join to the table that contains the blob_id's

But at a more basic level, don't you think you should test your connections,
etc, with a SINGLE call to a simple sproc first, instead of jumping right
into a 200 call loop?? In programming, we get the basics working first, then
move onto more difficult logic, based on exisitng, working logic.

Sounds like you don't even have your connection working yet. There are tons
of examples in the help files. Look in the help for the Connection object
for example.

Jeff
"Jerry" <je*******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
The query does work in the analyzer. The sp takes the following
parameters:
"EXEC saveNtext2file FileName, tableName, columnName, WhereCondition".
The sp uses the parameters to build the SQL statement: "set @sql =
N'SELECT @c =(select DATALENGTH('+@column+')/2 from '+@table+'
'+@where+')'"

What I ended up doing is exporting all of the Blob_IDs and putting them
into an array. Then using a for loop I step through the array and call
the sp each time.

I'm more used to classic asp but I'll be doing more and more .net work
in the near future. Do you have any suggstions on where I can go to get
some good resources for learning .net (online, books, etc.)?

Thanks,

--
Jerry

May 2 '06 #8

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

Similar topics

4
by: Michael Trosen | last post by:
Hi Everyone, I hope someone can help, I'm pretty new to pro*c programming. I have the following application setup: a pro*c program calls a stored procedure and recieves a cursor back: the...
3
by: Jarrod Morrison | last post by:
Hi all Im relatively new to using stored procedures and im not sure if it is possible to do what I am trying to do so any help here is greatly appreciated. I am using the variable @MachineName...
5
by: Jarrod Morrison | last post by:
Hi All Im unsure of how to use vb to read the results of my stored procedure. Ive included the stored procedure at the end of this message for reference. Basically the stored procedure will...
9
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
2
by: andres | last post by:
hi all, thanks for everyone for your support! i have a report being built in studio 2005. It calls a stored procedure. The SP runs a query and gets a data set which then loops through to get...
4
by: lokesh.kris | last post by:
I'm having a tough time trying to run this stored procedure in DB2 UDB 8.1 environment. Can someone help me out here. All I'm trying to do here is update an indicator 'N' if it is a NULL....
1
by: jason | last post by:
I've got this really simple table with two fields book: name varchar(50) phone varchar(50) and simple stored procedure jcp1: ALTER procedure "jcp1" @name varchar(50) AS
4
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...
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...
0
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,...
0
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...

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.