2011-10-18 19 views
0

私は動的データを使用しています。ユニークキーと他のデータベースレベルの検証を適用するには、SubmitChanges関数をオーバーライドしました。正規表現で値を抽出する

これは私に最終的なユーザーに示すことができるオリジナルのエラーを与えます。

Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America). 
The statement has been terminated. 

私はメッセージの次のキーワードを必要とする:しかし、このメッセージは、このようなものです

    1. UNIQUE KEY UK_CountryName_Country
    2. アメリカ

    私は、次の書かれています正規表現:

    System.Text.RegularExpressions.Regex.Split(e.Message.ToString(), "^Violation of (UNIQUE KEY){1} constraint '([a-zA-Z_]*)'. Cannot insert duplicate key in object 'dbo.([a-zA-Z]*)'. The duplicate key value is ") 
    

    私は#1 &#2、しかし#3を得ることに成功しました。

    誰でも助けてくれますか?また、それを達成するためのクリーンな方法がありますか? FYI:他のタイプのデータベースエラーも捕捉しています。

    おかげ

  • 答えて

    1

    あなたの例ではression、あなたが代わりにあなたが尋ねたようAmericadbo.CountryからCountryと一致するようにしようとしていたように見えたので、私は両方の私の表現/コードに含まれる:式は、エラーの種類の非常に寛容になるように設計されて

    Dim ExpressionText As String = "^Violation of (?<KeyType>.*?) constraint '(?<ConstraintName>[^']*)'\.[^']*'(?<ObjectName>[^']*)'[^(]*\((?<KeyValue>[^)]*)\)" 
    Dim SearchText As String = "Violation of UNIQUE KEY constraint 'UK_CountryName_Country'. Cannot insert duplicate key in object 'dbo.Country'. The duplicate key value is (America). " _ 
               & vbCrLf & "The statement has been terminated." 
    Dim regex As New System.Text.RegularExpressions.Regex(ExpressionText) 
    Dim mc As System.Text.RegularExpressions.Match = regex.Match(SearchText) 
    
    Response.Write(mc.Groups("KeyType").Value)   ' writes UNIQUE KEY 
    Response.Write(mc.Groups(1).Value)     ' writes UNIQUE KEY 
    Response.Write(mc.Groups("ConstraintName").Value) ' writes UK_CountryName_Country 
    Response.Write(mc.Groups(2).Value)     ' writes UK_CountryName_Country 
    Response.Write(mc.Groups("ObjectName").Value)  ' writes dbo.Country 
    Response.Write(mc.Groups(3).Value)     ' writes dbo.Country 
    Response.Write(mc.Groups("KeyValue").Value)   ' writes America 
    Response.Write(mc.Groups(4).Value)     ' writes America 
    

    メッセージに違反する可能性がある他の種類のキー制約があるためです。私は可能性のあるエラーのリストを見つけることが、ここでは、式の内訳であることができませんでした。

    ^Violation of     # Match literal text 
    (?<KeyType>.*?) constraint  # Capture the words that come before " constraint" 
    '(?<ConstraintName>[^']*)'\. # Capture whatever is inside the single quotes: ' 
    [^']*       # Match anything up to the next single quote 
    '(?<ObjectName>[^']*)'   # Capture whatever is inside the single quotes 
    [^(]*       # Match anything up to the next open parentheses: ( 
    \((?<KeyValue>[^)]*)\)   #Capture whatever is inside the parentheses 
    

    名前付きキャプチャグループを使用しない場合は?<Something>のように見える部分を取り除くことができ、数字だけを使用したい

    +0

    誰かが素晴らしいRegexビルダーであることをあなたに伝えましたか?最初の試みで成功。 Regexを学ぶためのリンクも教えてください。ありがとう。 – iMatoria

    +0

    非常に良いチュートリアル(正規表現について私が知っていることの90%を教えてくれました)は[Regular-Expressions.info](http://www.regular-expressions.info)です - そのサイトの作者は、 RegexBuddyと呼ばれるソフトウェアは、あらゆる種類のRegexesのデバッグをエミュレートしてサポートすることに優れています(すべての種類の言語/実装 - あるいは頻繁に呼び出されるような "正規表現の味") –

    1

    最後の値が終わり近くにあるので、あなただけの最後の期間と一致しないように注意しながら、行の末尾に一致させることができます。

    ".... The duplicate key value is (.*)\.$" 
    
    +0

    まあ、本当にいいですね。私はこれらのエラーをキャッチするためにいくつかの他の作業を探しています。私はそれを取得しない場合、私は答えとしてマークします。回答ありがとうございます。とても有難い。 – iMatoria

    0
    ^Violation\s+of\s+(.*?)'(.*?)'.*?\((.*)\) 
    

    を私は気づいていませんよすべての可能なエラーの様子を示しますが、上の正規表現はキャプチャします:

    Group 1: UNIQUE KEY constraint  
    Group 2: UK_CountryName_Country 
    Group 3: America 
    
    関連する問題