Telephone fields and CPF in SQL?

3

I'm doubtful what kind of variable to create for the CPF and Phone fields in the database, they should be saved like this:

(34) 9652-5214
134.124.214-47 

With nchar would not complicate the search? But in the case of the nchar, how do I put it so he can not store letters? And how do I put it when the user searches, can search only for numbers, without () etc? Will only put masquerade in the search field?

    
asked by anonymous 03.07.2015 / 22:05

3 answers

3

In C # there is a component called MaskedTextBox . In it you can specify a mask for the field. For example in your phone field the mask may be: (##) #####-#### in the Mask property. He will only accept numbers. At the time of reading the value, you will use the following code:

this.maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
String telefone = this.maskedTextBox1.Text;

So you will only have numbers in the String telefone , so save to the database.

    
03.07.2015 / 22:34
1

What is the database?

For the CPF I recommend using the Char type and specifying a size of 11 characters, without saving the periods and the hyphen.

For the phone I advise a VARCHAR field of maximum size 11, since in some regions of the country they use one more field or one field less.

ALTER TABLE tabela ADD COLUMN cpf CHAR(11);
ALTER TABLE tabela ADD COLUMN telefone VARCHAR(11);

I hope this answer has helped, any questions are available.

    
03.07.2015 / 22:12
1

All the checks you can do before storing the information in the database, for example a form may have mascara that adds the (xx) xxxx-xxxx, but you make a treatment to remove the other characters that are not number , thus storing only number in a varchar in the database.

Remembering if you want the value to be checked before sending to confirm that you are sending only correct digits, use regular expression to recognize the string something like ([0-9]{2})\s[0-9]{4}-[0-9]{4} I'm not saying that this regular expression is right, but it's something very close to what you can use, so it will force you to have two numbers in the DDD in parentheses and 8 followed numbers separated by -.

This already guarantees, then a split or tokenizer depending on what you use to separate and remove non-digits, and concatenates everything else as a number and saves it to the database.

When accessing, to display the data in visual form just create a mask again to apply over the restored value. As a database the values need to be correct, the treatments who do and always the program to get the value and demonstrate, at least the best way for you to think is this.

    
04.07.2015 / 00:47