[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.odbcnet

How can I send string with single quote in it to SQL Server??

Marlene A. Roman

6/5/2002 7:22:00 PM


Hello!

How's everybody???

I'm working with SQL server and I'm executing a stored
procedure assigning the sql statement to the CommantText
property and executing the ExecuteScalar method of the
SqlCommand object

My Sql statement looks like this

sql="sp_my_storeprocedute '123','Margot O'hara'"

The problem is that because SQL Server use the single
quote (') to delimits the strings, my data can't have any
single quote because it cause an error.

I'm sure that a lot of people have had this problem. How
can I deal with that???

How can I send the data from VB with the single quote?


I appreciate any input

Thank you in advance


Marlene A. Roman


2 Answers

(Nam Kang)

6/6/2002 1:30:00 AM

0

Marlene,

call this function and it will fix your string


Function SingleQuoteFix(strIn)
Dim y
If InStr(strIn, "'") <> 0 Then
For y = 1 To Len(strIn)
If Mid(strIn, y, 1) = "'" Then
SingleQuoteFix = SingleQuoteFix + "''"
Else
SingleQuoteFix = SingleQuoteFix + Mid(strIn, y, 1)
End If
Next
Else
SingleQuoteFix = strIn
End If
End Function

Hope that helps,
Nam

This posting is provided "AS IS" with no warranties, and confers no rights.

CT

6/8/2002 8:00:00 AM

0

Since we're working with the .NET Framework, would it not be easier to just
use the Replace method of the String class?

--
Carsten Thomsen
Database Programming with C#
(http://www.apress.com/catalog/book/...)
http://www.dotnetse...

"Nam Kang" <nkang@online.microsoft.com> wrote in message
news:tDAvbjODCHA.1648@cpmsftngxa07...
> Marlene,
>
> call this function and it will fix your string
>
>
> Function SingleQuoteFix(strIn)
> Dim y
> If InStr(strIn, "'") <> 0 Then
> For y = 1 To Len(strIn)
> If Mid(strIn, y, 1) = "'" Then
> SingleQuoteFix = SingleQuoteFix + "''"
> Else
> SingleQuoteFix = SingleQuoteFix + Mid(strIn, y, 1)
> End If
> Next
> Else
> SingleQuoteFix = strIn
> End If
> End Function
>
> Hope that helps,
> Nam
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>