Re: Return value from recordset
From: Bob Barrows [MVP] (reb01501_at_NOyahoo.SPAMcom)
Date: 01/05/05
- Next message: Joe Fawcett: "Re: field.value"
- Previous message: LBT: "Return value from recordset"
- In reply to: LBT: "Return value from recordset"
- Next in thread: LBT: "Re: Return value from recordset"
- Reply: LBT: "Re: Return value from recordset"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 5 Jan 2005 06:32:25 -0500
LBT wrote:
> Good day experts and seniors,
You forgot to tell us what type and version of database you are using. This
is ALWAYS relevant. Please don't leave it out of your future posts.
>
> I have a SQL INSERT statement at my ASP page executed using ADO
> recordset. Eg: Set oRs = rs("INSERT INTO myTable VALUES ('Testing',
> 1)", objConn)
First of all, you should not be using an expensive recordset object to run a
query that returns no records. You should use the connection's Execute
method for that, like this:
dim sSQL
sSQL = "INSERT INTO myTable VALUES ('Testing',1)"
objConn.Execute sSQL,,129
objConn.Close: Set objConn=Nothing
The 129 is a combination of two constants: adCmdText (1) which tells ADO
that you are executing a string containing a SQL statement (always tell ADO
what the command type is; don't make it guess), and adExecuteNoRecords
(128), which tells ADO not to bother constructing a recordset because you
don't expect to get records back from your query. 1+128=129
You can read more about this here:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdobjconnection.asp
which links to here:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthcnnexecute.asp
>
> My question here is I want to make sure that the INSERT statement is
> successfully run to insert the record to table.
The second argument of the Execute method (the one I left out in my first
example) can be a variable that will be passed ByRef, and will contain the
number of records affected by your query after it executes. So, this will do
what you want:
dim lRecs
lRecs = 0
dim sSQL
sSQL = "INSERT INTO myTable VALUES ('Testing',1)"
objConn.Execute sSQL,lRecs,129
Response.Write lRecs & " record(s) inserted"
objConn.Close: Set objConn=Nothing
Bob Barrows
-- Microsoft MVP - ASP/ASP.NET Please reply to the newsgroup. This email account is my spam trap so I don't check it very often. If you must reply off-line, then remove the "NO SPAM"
- Next message: Joe Fawcett: "Re: field.value"
- Previous message: LBT: "Return value from recordset"
- In reply to: LBT: "Return value from recordset"
- Next in thread: LBT: "Re: Return value from recordset"
- Reply: LBT: "Re: Return value from recordset"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|