본문 바로가기

.NET/Winforms

[WINFORMS] DB Data Type 과 Text control 글자수 일치

DB Data Type 과 Text control 글자수 일치

  • DB 에는 각 컬럼에 따라 DATA TYPE 을 설정한다 (INT, NVARCHAR(50), .ETC)
  • Text control 에서 string 길이 를 DB 와 일치시켜주기 위한작업이 필요하다.
  • 단순 Length 로 만 처리하는것은 잘못된 방법이다. 왜냐하면 언어에 따라 기본적으로 차지하는 Byte 수가 다르기 때문이다. 

#1

  • 만약 DB 특정 컬럼이 NVARCHAR(50) 으로 설정되어 있는경우 
  • 해당 텍스트를 DB 에 설정된 Encoding 스타일에 따라 해당 문자열을 변환한다.
string userInput = textBox.Text; // Get the text from the TextBox

// Specify the encoding (UTF-8 in this example, adjust as needed)
Encoding encoding = Encoding.UTF8;

// Get the byte length of the input string using the specified encoding
int byteLength = encoding.GetByteCount(userInput);

// Check if the byte length exceeds the allowed length (50 bytes in this case)
if (byteLength > 50)
{
    MessageBox.Show("Text byte length exceeds the maximum allowed (50 bytes). Please shorten the text.");
    return; // Exit the method, do not proceed with the insertion
}

 


There might be incorrect information or outdated content.