2011-12-24 11 views
1

まず、すべてを試したところ、varbinaryフィールドが正しく更新されない理由を理解できません。唯一のバイト配列の最後のバイトがフィールドに保存された1728バイトのうちC#で呼び出されるストアドプロシージャを使用してBytearrayを挿入する

...

次のように私は私のバイト配列を生成します。

public static byte[] StringToByteArray(String hex) 
{ 
    int NumberChars = hex.Length; 
    byte[] bytes = new byte[NumberChars/2]; 
    for (int i = 0; i < NumberChars; i += 2) 
     bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16); 
    return bytes; 
} 

私はまた、以下のいずれかを試してみました:データを保存する

public static byte[] ParseHex(string hex) 
{ 
    int offset = hex.StartsWith("0x") ? 2 : 0; 
    if ((hex.Length % 2) != 0) 
    { 
     throw new ArgumentException("Invalid length: " + hex.Length); 
    } 
    byte[] ret = new byte[(hex.Length - offset)/2]; 

    for (int i = 0; i < ret.Length; i++) 
    { 
     ret[i] = (byte)((ParseNybble(hex[offset]) << 4) 
         | ParseNybble(hex[offset + 1])); 
     offset += 2; 
    } 
    return ret; 
} 

static int ParseNybble(char c) 
{ 
    if (c >= '0' && c <= '9') 
    { 
     return c - '0'; 
    } 
    if (c >= 'A' && c <= 'F') 
    { 
     return c - 'A' + 10; 
    } 
    if (c >= 'a' && c <= 'f') 
    { 
     return c - 'a' + 10; 
    } 
    throw new ArgumentException("Invalid hex digit: " + c); 
} 

私のC#のコードはこれです:

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_Conn"].ConnectionString)) 
{ 
    byte[] to_store = StringToByteArray(inventory); 

    //State the Stored Proc and add Values to 'cmd' to pass to the Stored Proc 
    SqlCommand cmd = new SqlCommand("_USP_store", conn); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@A", TB_A.Text); 
    cmd.Parameters.Add("@B", SqlDbType.VarBinary, 1728).Value = to_store; 

    try 
    { 
    // Open Connection and execute Stored Proc 
    conn.Open(); 
    cmd.ExecuteNonQuery(); 
    C2_Wipe_Message.Text = "Storing success"; 
    C2_Wipe_Message.ForeColor = Color.FromArgb(0, 0, 255, 0); 

    } 
    catch 
    { 
    C2_Wipe_Message.Text = "An error occured.."; 
    C2_Wipe_Message.ForeColor = Color.FromArgb(0, 255, 0, 0); 
    } 
    finally 
    { 
    if (conn.State == System.Data.ConnectionState.Open) 
    { 
     //Close connection IF open 
     conn.Close(); 
    } 
    } 
} 

私はそれを文字列として送信しました。普通のバイナリとして送信しました。16進バイト配列などで送信しました。

私は、sqlのwhileループを使用することを前提としています。それを保存し、それが最後のバイトは、常に代わりにバイト配列の最初のバイトで保存されている理由を説明していません..私は、これは腹立たしいです原因啓発してください

* SQLのSP

@A varchar(10), 
@B varbinary(1728) 

    AS 

UPDATE Invenotry 
SET A = @B 
WHERE (Name = @A) 
+1

は、私はSQLでループを使用すると、私には間違って聞こえるので、問題がある期待するSQLを見ることができます。 – Hogan

+0

ループを使用していませんが、SQLプロシージャを出力します – Raskaroth

+0

'[Invenotry]。[A]'フィールドのタイプは何ですか? – Hogan

答えて

3

あなたのsqlはこれでなければなりません:

UPDATE Invenotry 
SET B = @B 
WHERE A = @A 

また、パラメータのコンストラクタのフルバージョンを試すことができます。

SqlParamter param = new SqlParameter("@B", SqlDbType.VarBinary, 1728, ParameterDirection.Input, 
    // we have these parameters but they are ignored for input types 
    false, 0, 0, null, DataRowVersion.Current, 
    // the data 
    to_store); 

cmd.Parameters.Add(param); 
+0

ここでは、16進文字列をバイト配列に変換する方法私は上記の2つの方法を使用しますか?または、合計で別の方法を使用しますか? – Raskaroth

+0

WHERE節の '('と ')' arroundが気に入らないようですが、大きな問題がフィールドに適切な名前を与えていることに気付きました。私はこれに応じてフィールド名を変更し、varbinaryフィールドに適切なサイズを割り当てました(1728ではなく1726 ..)。まだバイト配列への変換に興味がありますが、 – Raskaroth

+0

最初のものしかし、それは開始の '0x'をチェックしません - あなたは' 0x'の開始を見ますか? – Hogan

関連する問題