首先我尝试了一切,并且无法理解为什么它不会正确更新我的varbinary字段。通过在C中调用的存储过程将Bytearray插入到SQL中#
出的1728个字节只有字节数组中的最后一个字节保存到外地...
我产生我的字节数组如下:
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();
}
}
}
我已经把它作为一个字符串,我已经把它作为纯二进制,我已经把它作为一个十六进制字节数组等
我的假设是在SQL中使用while循环,以存储它,但是这并不能解释为什么最后一个字节总是被保存,而不是字节数组的第一个字节,请赐教因为这是真气..
* SQL SP
@A varchar(10),
@B varbinary(1728)
AS
UPDATE Invenotry
SET A = @B
WHERE (Name = @A)
让我们看看我想到的问题是存在的,因为在SQL中使用循环听起来我错了的SQL。 – Hogan
不使用循环,但我会得到SQL过程 – Raskaroth
字段的类型是什么?[Invenotry]。[A]'? – Hogan