473,396 Members | 1,599 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,396 software developers and data experts.

Heavily used functions

I do a alot SQL statements, these statements require alot of quoted
strings. to this end I have a small one line routine:
private string QuotedStr(string Value)
{
return "'"+Value+"'";
}
I usel this function many , many times.
the probem I have is I need to write this small function (along with others)
in each class, web form, etc I use.
I would really like to write this in one class and use it ervery place. I
looked into static functions, but you land up referenceing the function by
MyClass.QuotedStr(). well this is alittle better but,
I guess what I am looking for is a way to globalize this function. It would
be great if I could some how just add it to the string class, anyway any
ideas would be appriciated.
Phil.

Nov 16 '05 #1
2 4822
No, you need to do exactly what you outlined: make it a static method
in some class. Here is mine:

/// <summary>
/// Formats a database value into a string format suitable for
query operations.
/// For example, strings are quoted.
/// </summary>
/// <param name="columnValue">The value to be used in a SQL
statement.</param>
/// <returns>A string representation of that value.</returns>
public static string ValueToSQLString(object columnValue)
{
switch (Type.GetTypeCode(columnValue.GetType()))
{
case TypeCode.String:
return "'" + EscapeText(columnValue.ToString()) +
"'";
default:
return EscapeText(columnValue.ToString());
}
}

/// <summary>
/// Escapes the given text so that it can appear within single
quotes in a
/// SQL statement.
/// </summary>
/// <param name="textToEscape">The string to escape.</param>
/// <returns>The escaped string, ready to be included in a
/// SQL statement.</returns>
public static string EscapeText(string textToEscape)
{
string backslashesEscaped = textToEscape.Replace(@"\",
@"\\");
string backslashAndSingleQuoteEscaped =
backslashesEscaped.Replace(@"'", @"\'");

return backslashAndSingleQuoteEscaped;
}

Remember that you have to escape any quote characters and backslash
characters in your Value in order to prevent SQL injection attacks!
This is the value of always calling a central method rather than doing
it in every form!

Nov 16 '05 #2
Phil Barber <ph*********@ajg.com> wrote:
I do a alot SQL statements, these statements require alot of quoted
strings.
Can you not use parameterised statements? That would be a lot better in
general - potentially faster, less garbage created, and safer from SQL
injection attacks.
to this end I have a small one line routine:
private string QuotedStr(string Value)
{
return "'"+Value+"'";
}
I usel this function many , many times.
the probem I have is I need to write this small function (along with others)
in each class, web form, etc I use.
I would really like to write this in one class and use it ervery place. I
looked into static functions, but you land up referenceing the function by
MyClass.QuotedStr(). well this is alittle better but,
I guess what I am looking for is a way to globalize this function. It would
be great if I could some how just add it to the string class, anyway any
ideas would be appriciated.


You certainly can't add it to the string class.

I can't remember for sure, but I *think* that in C# 2.0 you could do
something like:

using static MyClass;

along with your other using directives, and that that would allow you
to just call QuotedStr. For the moment, just use MyClass.QuotedStr(...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

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

Similar topics

2
by: Erwin Moller | last post by:
Hi group, I have an includefile that I (ahum) include in many scripts. It contains only functions I need now and then. Now I was wondering how things work behind the scenes. Is the whole...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
4
by: kim jørgensen | last post by:
Hi i am trying to write a email clint for my labtop. my problem is that am not always on the same network and can not always reach the same Smtp server- so i am looking for a funcktion which can...
5
by: MR. Ask | last post by:
Hallo. I have a question. For what are used following functions MM_swapImgRestore, MM_preloadImages, MM_findObj(n,d), MM_swapImage(). I would be grateful for the answer or source where I could...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
4
by: topdawg147 | last post by:
This question might be for another group, but since I use vb.net, I thought I'd ask here.... I'm looking to clean up some code. Is there a development tool to tell me which functions are not...
9
by: Simon | last post by:
Hi, I have written an ActiveX object to resize images and upload them to a database, this all works fine but when I close internet explorer the process iexporer.exe is still running in my task...
7
by: Xah Lee | last post by:
I had a idea today. I wanted to know what are the top most frequently used functions in the emacs lisp language. I thought i can write a quick script that go thru all the elisp library locations...
5
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I am developing a new application. Since it is being developed from scratch, I want to implement a lot of threading - especially for my SQL calls. The problem I noticed is that I can't seem to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.