PC Review


Reply
Thread Tools Rate Thread

DB field name valid

 
 
mp
Guest
Posts: n/a
 
      27th Jan 2011
to ensure string is legal field name, first thought is below:

private string ConvertToDBFieldName ( string str )
{//disallowed characters in database fieldname ` . ! ’ [ ] " * ?
StringBuilder sb = new StringBuilder ( str );
sb.Replace ( " ", "_" );
sb.Replace ( "`", "" );
sb.Replace ( ".", "" );
sb.Replace ( "!", "" );
sb.Replace ( "’", "" );
sb.Replace ( "[", "" );
sb.Replace ( "]", "" );
sb.Replace ( "\"", "" );
sb.Replace ( "*", "" );
sb.Replace ( "?", "" );
return sb.ToString ( );
}
comments?
thanks
mark


 
Reply With Quote
 
 
 
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 1/27/11 12:03 PM, mp wrote:
>> to ensure string is legal field name, first thought is below:
>>
>> private string ConvertToDBFieldName ( string str )
>> {//disallowed characters in database fieldname ` . ! ? [ ] " *
>> ?
>> StringBuilder sb = new StringBuilder ( str );
>> sb.Replace ( " ", "_" );
>> sb.Replace ( "`", "" );
>> sb.Replace ( ".", "" );
>> sb.Replace ( "!", "" );
>> sb.Replace ( "?", "" );
>> sb.Replace ( "[", "" );
>> sb.Replace ( "]", "" );
>> sb.Replace ( "\"", "" );
>> sb.Replace ( "*", "" );
>> sb.Replace ( "?", "" );
>> return sb.ToString ( );
>> }
>> comments?

>
> Another example of where the Regex class might be nicer. But even without
> Regex, it can be somewhat simpler/more-readable:
>
> private static HashSet<char> _removeChars =
> new HashSet(new char[] { '`', '.', '!', '?', '[', ']', '\\', '*',
> '?' });
>
> private string ConvertToDBFieldName(string str)
> {
> StringBuilder sb = new StringBuilder(str.Length);
>
> foreach (char ch in str)
> {
> if (ch == ' ')
> {
> sb.Append('_');
> }
> else if (!_removeChars.Contains(ch))
> {
> sb.Append(ch);
> }
> }
>
> return str;
> }
>
> The use of repeated calls to Replace() is not quite as bad when using
> StringBuilder as if you were using String. But it's still potentially
> expensive, and in any case always excessively so. IMHO, simply filtering
> as you copy characters is clearer and more efficient. What's not to like?
>
>
> Pete


very nice!
:-)
thanks
mark
ps I assumed you meant > return sb.ToString();
and it seemed to want HashSet<char>(new char[]

i'll have to give some thought to the regex version..
still not in my head yet.
:-)
i'm sure there's a simple negating character grouping construct...
i'll look it up
thanks again
mark


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      27th Jan 2011

"mp" <(E-Mail Removed)> wrote in message
news:ihqsg4$qvl$(E-Mail Removed)...

> i'm sure there's a simple negating character grouping construct...


[^<your negative character list>]


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      27th Jan 2011

"mp" <(E-Mail Removed)> wrote in message
news:ihqqr7$mdq$(E-Mail Removed)...

> to ensure string is legal field name, first thought is below:


Field name for what DBMS? Access allows spaces, for example, as does SQL
Server. Also, and unfortunately, SQL Server allows at least one of the
bracket characters ( [ & ] ) inside a field name. I've actually run across
one before. I cursed the table creator with everything I had. Hopefully he
at least contracted some boils....


 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"Jeff Johnson" <(E-Mail Removed)> wrote in message
news:ihqsop$vp1$(E-Mail Removed)...
>
> "mp" <(E-Mail Removed)> wrote in message
> news:ihqqr7$mdq$(E-Mail Removed)...
>
>> to ensure string is legal field name, first thought is below:

>
> Field name for what DBMS?


sqlite

Access allows spaces, for example, as does SQL
> Server.


i know they're allowed, i just didn't want them

Also, and unfortunately, SQL Server allows at least one of the
> bracket characters ( [ & ] ) inside a field name. I've actually run across
> one before. I cursed the table creator with everything I had. Hopefully he
> at least contracted some boils....


:-)


 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"Big Steel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 26 Jan 2011 22:03:49 -0600, "mp" <(E-Mail Removed)> wrote:
>> to ensure string is legal field name, first thought is below:

>
>
>> private string ConvertToDBFieldName ( string str )
>> {//disallowed characters in database fieldname ` . ! ’ [ ]

> " * ?

[]>> }
>> comments?

>
> It's overkill validation.
>
>


actually this just runs once, i'm just massaging some existing strings
and making them palatable for a CREATE tbl (fldName T,....) string
I knew the input strings had some flaky chars so just throwing together
a little cleanup temp method



 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 1/27/11 12:32 PM, mp wrote:
>> [...]
>> ps I assumed you meant> return sb.ToString();
>> and it seemed to want HashSet<char>(new char[]

>
> Yes to both. The hazards of posting from a Mac. Fortunately,
> mistakes like those are easy to detect and fix.


yep, just tagged that on for sake of archives
thanks again
mark


 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"Jeff Johnson" <(E-Mail Removed)> wrote in message
news:ihqskl$v88$(E-Mail Removed)...
>
> "mp" <(E-Mail Removed)> wrote in message
> news:ihqsg4$qvl$(E-Mail Removed)...
>
>> i'm sure there's a simple negating character grouping construct...

>
> [^<your negative character list>]
>


thats' what I thought i remembered but had to look it up again to be sure
seems i have to put the carat outside ^[<your negative character list>]
i tried this
Regex rgx = new Regex ( @"^[\s()'-]" );

sb.Append ( rgx.Match ( str ) );

but that wiped everything out

so i tried

foreach (char c in str)
{
if (!rgx.IsMatch ( c.ToString ( ) ))
{
sb.Append ( c.ToString ( ) );
}
}

which worked but i don't think that's how i'm supposed to have to do it with
regex...i have to go back to my articles and re-read

thanks

mark


 
Reply With Quote
 
mp
Guest
Posts: n/a
 
      27th Jan 2011

"mp" <(E-Mail Removed)> wrote in message
news:ihrs93$7n7$(E-Mail Removed)...
>
> "Jeff Johnson" <(E-Mail Removed)> wrote in message
> news:ihqskl$v88$(E-Mail Removed)...
>>
>> "mp" <(E-Mail Removed)> wrote in message
>> news:ihqsg4$qvl$(E-Mail Removed)...
>>
>>> i'm sure there's a simple negating character grouping construct...

>>
>> [^<your negative character list>]
>>

>
> thats' what I thought i remembered but had to look it up again to be sure
> seems i have to put the carat outside ^[<your negative character list>]


no you big dummy that means beginning of string...
jeff was right
but for some reason my mangling appeared to work????
i'll have to look at this some more

:-\


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      28th Jan 2011
On 26-01-2011 23:36, Jeff Johnson wrote:
> "mp"<(E-Mail Removed)> wrote in message
> news:ihqqr7$mdq$(E-Mail Removed)...
>> to ensure string is legal field name, first thought is below:

>
> Field name for what DBMS? Access allows spaces, for example, as does SQL
> Server. Also, and unfortunately, SQL Server allows at least one of the
> bracket characters ( [& ] ) inside a field name. I've actually run across
> one before. I cursed the table creator with everything I had. Hopefully he
> at least contracted some boils....


Better to pick names that can be expected to be valid in
most/all databases.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the value you entered isn't valid for this field Jsnyd Microsoft Access 2 16th Nov 2009 04:48 PM
Value you entered isn't valid for this field =?Utf-8?B?UmljaCBE?= Microsoft Access 5 25th Jul 2007 09:26 PM
the value you entered isn't valid for this field =?Utf-8?B?cGhpbGxpcDk=?= Microsoft Access Forms 3 19th Jul 2005 04:51 PM
value not valid for field dana Microsoft Access Getting Started 1 21st Oct 2004 10:31 PM
Value not valid for a field Barb Microsoft Access Reports 3 19th Jan 2004 12:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:58 AM.