将字节数组插入sql server

时间:2013-12-26 07:42:14

标签: c# sql sql-server

我正在尝试插入一个将二进制数组带入sql server的sql查询,但它无法正常工作

public static void UpdateImage(int imageid, byte [] binaryData)
{
    string query = "Update images set IMAGE_BINARIES="+binaryData+" where IMAGE_ID="+imageid;
    new SQLHelper(SQLHelper.ConnectionStrings.KernelAccountConnectionString).Insert(query);

}

IMAGE_BINARIES的类型为varbinary(max)

1 个答案:

答案 0 :(得分:3)

使用此示例:

        public static void LogActivity( byte[] data)
        {

                var connection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["LogConnectionString"].ConnectionString);
                var command = new SqlCommand { Connection = connection, CommandType = CommandType.Text };
                try
                {
                    command.CommandText = @"
insert into Logs (Data)
values ( @data)
";
                    command.Parameters.Add("@data", SqlDbType.VarBinary, data.Length).Value = data;
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    connection.Dispose();
                }
                catch (Exception ex)
                {
                    Log(ex);
                }

    }