ASP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingASP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 9th, 2003, 01:53 PM
vbc_bt vbc_bt is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Victoria, B.C.
Posts: 1 vbc_bt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ASP/ADO Direct SQL Insert Error With Escaped Quotes

Hello! Hopefully someone can help me with my problem! I'm using ASP/ADO with MSSQL Server 2000 and when I'm using direct SQL to Insert a string to my database I'm getting an error when the Escaped (SQL Encoded) string exceeds the max length of the db column.

I know this sounds straight forward but consider the following example if you will:

For illustrative purposes let's say we have a table with just one column like this:

value1 Varchar(10)

If you open a Query analyzer and perform the following query:

INSERT INTO my_table (value1) Values ('12345678''9')

The Insert is executed and no error occurs. If you query the database and look at the stored value it is displayed as: 12345678'9

The same is true if we're Inserting or Updating the same field using ASP/ADO and a simple stored procedure. ADO automatically escapes the single quotes (in the above example) for you and the Insert/Update is fine.

The PROBLEM and the reason for this long winded post is if you try to use ASP/ADO and direct SQL to perform the same Insert/Update as the example above. Consider the following:

strQuery = "UPDATE my_table SET value1 = '12345678''9'"
Set alphaRS = alphaConn.Execute(strQuery,, 1)

The above ASP code fails because the Length of the SQL Encoded string is greater than 10 characters; it is 11 characters with our escape sequence.

Aside from converting all Direct SQL Insert/Updates to stored procedures or doubling the Max Length's of my database fields, neither of which are acceptable solutions, what can I do?!! Why doesn't ADO handle this?

Many thanks to anyone who maybe be able to offer me a solution!

Cheers!

Reply With Quote
  #2  
Old August 2nd, 2003, 02:37 PM
rdoekes rdoekes is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Strasbourg, France
Posts: 181 rdoekes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 7
Send a message via AIM to rdoekes Send a message via Yahoo to rdoekes
Workaround

I see your point. I mocked up a quick test and the results are as you described.

Here is a quick workaround that might be usefull. The quote character in T-SQL has the ASCII value of 39. So you can think of a global replacement function which replaces all occurances of the quote in string with the CHAR(39) function. I tested this and the results were promising.
PHP Code:
Function ReplaceQuote(sText)
  
Dim sOut
  sOut 
Replace(sText"'""'+ CHAR(39) +'")
  If 
Right(sOut2) = "+'" Then  sOut Mid(sOut1Len(sOut) -2)

  
ReplaceQuotes "'" &  sOut  "'"
End Function 

so your test string will become
PHP Code:
'12345678' CHAR(39) + '9' 
with this function
Hope this helps. With this you do not need to change your database or convert your direct inserts to stored procedures.

[note]: converting the direct inserts to stored procedures is a preferred solution:
- execution paths of all your queries is known therefore faster execution of your sql stamements,
- security is a lot tighter: you allow only access to stored procedures and not the underlying database
- you could use the robust transaction functionality of the sql server including table and row locking and such.
but that aside [/note]
__________________
- Rogier Doekes

Last edited by rdoekes : August 2nd, 2003 at 02:40 PM.

Reply With Quote
  #3  
Old August 4th, 2003, 09:40 AM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 8 m 57 sec
Reputation Power: 9
Send a message via ICQ to stumpy Send a message via MSN to stumpy
How interersting/annoying. Seems to me like a bit of a logic error in MS's ADO code. I fail to see a valid reason behind performing the char string length before escaping the string. Odd.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingASP Development > ASP/ADO Direct SQL Insert Error With Escaped Quotes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT