Google
 
Web itpro-blogger.blogspot.com
月曜日, 4月 14, 2008

[VB2005]文字サイズチェック

▼質問
データベースに文字列を格納する時に下記のエラーが出るんだ。

ORA-12899: 列"○×△□"."テーブル名"."フィールド名"の値が大きすぎます(実際: 256、最大: 255)

文字列のサイズをチェックをして、格納サイズより大きい時は

格納サイズより大きい部分は削除したいんだ。どうしたらできるんだい。

文字数のチェックはMaxLengthプロパティに値を入れておけば

自動的にやってくれてはいるんだけど...

▼回答
こんなのできたよ。

Public Shared Function ChkByte(ByVal strValue As String, ByVal byteSize As Integer) As Boolean
If (System.Text.Encoding.GetEncoding("Shift-JIS").GetByteCount(strValue) <= byteSize) Then ChkByte = True
End Function

Public Shared Function GetByte(ByVal strValue As String, ByVal byteSize As Integer) As String
Dim hEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift-JIS")
Dim bBytes As Byte() = hEncoding.GetBytes(strValue)
Dim count As Long
For i As Long = Len(strValue) To 1 Step -1
If (ChkByte(Mid(strValue, 1, Len(strValue) - count), byteSize)) Then
Return System.Text.Encoding.GetEncoding("Shift-JIS").GetString(bBytes, 0, Len(strValue) - count)
End If
count = count + 1
Next i
Return strValue
End Function

■参考サイト
VB.NET 全角・半角でのLEFT関数 - Insider.NET
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=24347&forum=7&start=8






Comments: コメントを投稿

<< Home

This page is powered by Blogger. Isn't yours?